Java Android活动生命周期——如何在重新启动活动时重置变量?

Java Android活动生命周期——如何在重新启动活动时重置变量?,java,android,lifecycle,android-lifecycle,Java,Android,Lifecycle,Android Lifecycle,我将尽可能简短地回答我的问题: 我有一个简单的活动,用来检查和显示设备是否支持GSM,是否有双SIM卡。我的问题是:-当应用程序退出时,第二个SIM卡打开,应用程序重新启动,复选框显示它仍然没有准备好。当使用第二张激活卡启动并关闭时,情况也是如此-当应用程序在task manager中被终止并再次启动时,它会在第一次显示SIM卡的正确状态,但当使用back键退出时,会出现与前一点中提到的相同问题-我清除了Android应用程序生命周期所有阶段的所有回调函数中此活动的所有变量,但问题仍然存在 我做

我将尽可能简短地回答我的问题:

我有一个简单的活动,用来检查和显示设备是否支持GSM,是否有双SIM卡。我的问题是:-当应用程序退出时,第二个SIM卡打开,应用程序重新启动,复选框显示它仍然没有准备好。当使用第二张激活卡启动并关闭时,情况也是如此-当应用程序在task manager中被终止并再次启动时,它会在第一次显示SIM卡的正确状态,但当使用back键退出时,会出现与前一点中提到的相同问题-我清除了Android应用程序生命周期所有阶段的所有回调函数中此活动的所有变量,但问题仍然存在

我做错了什么,或者不算数。提前谢谢你宝贵的帮助

贾尼

在此,活动代码为:

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;
}
}

clearVariables只需清除局部变量hasGsmCheckBox/isDualSimCheckBox,这不是复选框视图本身。要使复选框显示就绪,您必须使用CheckBox.setChecked(true),就像您在onStart方法上所做的一样

但是只有当活动不再可见时才会调用onStart()

我想您可能可以尝试将onStart()中的内容放入onResume()