Android Activity Life Cycle ---如何在重启Activity时重置变量?

时间:2014-06-18 19:54:03

标签: java android lifecycle android-lifecycle

我会尽量让我的问题保持简短:

我有这个简单的活动,用于检查和显示设备是否支持GSM并且是否具有双SIM卡。我的问题是: - 当退出应用程序时,然后打开第二个SIM卡并重新启动应用程序,复选框显示它仍未准备就绪。从活动的第二张卡开始并关闭时也是如此。 - 当应用程序在任务管理器中被终止时,再次启动它会在第一次显示SIM的正确状态,但是当退出后退键时,会出现同样的问题,如上一点所述。 - 我在Android应用程序生命周期的所有阶段的所有回调函数中清除了此Activity的所有变量,但问题仍然存在。

我做错了什么,或者不记得。感谢您提前提供的宝贵帮助

贾尼

以下是活动的代码:

package com.szilij.mymobilecontrols;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.TextView;


public class MainActivity extends Activity {

private CheckBox hasGsmCheckBox;
private CheckBox isDualSimCheckBox;
private String imeiSIM1;
private String imeiSIM2;
private boolean isSIM1Ready;
private boolean isSIM2Ready;
private boolean isDualSIM;
private TelephonyManager manager;
private TelephonyInfo telephonyInfo =null;

private void clearVariables() {
    hasGsmCheckBox  = null;
    isDualSimCheckBox = null;
    imeiSIM1 = null;
    imeiSIM2 = null;
    isSIM1Ready = false;
    isSIM2Ready = false;
    isDualSIM = false;
    manager = null;
    telephonyInfo =null;    
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    clearVariables();

    hasGsmCheckBox = (CheckBox)findViewById(R.id.checkBox1);
    isDualSimCheckBox = (CheckBox)findViewById(R.id.checkBox2);

    hasGsmCheckBox.setChecked(false);
    isDualSimCheckBox.setChecked(false);

    manager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);

    if (manager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
        hasGsmCheckBox.setChecked(true);


        telephonyInfo = TelephonyInfo.getInstance(this);

        imeiSIM1 = telephonyInfo.getImeiSIM1();
        imeiSIM2 = telephonyInfo.getImeiSIM2();

        isSIM1Ready = telephonyInfo.isSIM1Ready();
        isSIM2Ready = telephonyInfo.isSIM2Ready();

        isDualSIM = telephonyInfo.isDualSIM();
        isDualSimCheckBox.setChecked(isDualSIM);


        TextView tv = (TextView) findViewById(R.id.textView1);
        tv.setText(" IME1 : " + imeiSIM1 + "\n" +
                " IME2 : " + imeiSIM2 + "\n" +
                " IS DUAL SIM : " + isDualSIM + "\n" +
                " IS SIM1 READY : " + isSIM1Ready + "\n" +
                " IS SIM2 READY : " + isSIM2Ready + "\n");

        clearVariables();

    }
    else {
        hasGsmCheckBox.setChecked(false);
        isDualSimCheckBox.setChecked(false);
    }

    clearVariables();

    super.onStart();
}

@Override
protected void onRestart() {
    // TODO Auto-generated method stub
    clearVariables();

    super.onRestart();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    clearVariables();

    super.onPause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    clearVariables();

    super.onResume();
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    clearVariables();

    super.onStop();
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    clearVariables();

    super.onDestroy();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

1 个答案:

答案 0 :(得分:2)

clearVariables只清除局部变量 hasGsmCheckBox / isDualSimCheckBox ,这不是checkBox视图本身。要使CheckBox显示为准备好,您必须使用 checkBox.setChecked(true),就像您在onStart方法上所做的那样;

onStart()仅在活动不再可见时调用

请参阅http://i.stack.imgur.com/YvmA8.png

我想你可能会尝试将 onStart()中的内容放入 onResume()中。