2014-06-18 108 views
1

我会尽量保持我的问题尽可能简短:Android活动生命周期---如何重新启动活动时重置变量?

我有这个简单的活动,用于检查和显示设备是否支持GSM并且有双SIM卡。我的问题是: - 当应用程序退出时,第二张SIM开启,应用程序重新启动,复选框显示它尚未准备就绪。使用活动的第二张卡启动并关闭时也是如此。 - 当应用程序在任务管理器中被终止并再次启动时,它会首次显示SIM的正确状态,但当用退回键退出时,会出现与上一点相同的问题。 -I清除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; 
} 
} 

回答

2

clearVariables只是清除本地变量hasGsmCheckBox/isDualSimCheckBox,这是不是复选框视图本身。为了使CheckBox显示就绪,你必须使用checkBox.setChecked(true),就像你在onStart方法上做的一样;

在onStart()当活动不再可见时才会调用

http://i.stack.imgur.com/YvmA8.png

我想你也许可以尝试把在在onStart()什么成的onResume( )