我會盡量保持我的問題儘可能簡短: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;
}
}