簡體   English   中英

Android的活動生命周期 - 如何在重新啟動的活動重置變量?

[英]Android Activity Life Cycle — how to reset variables upon restarting an Activity?

我將盡量使我的問題簡短:

我有一個簡單的活動,該活動用於檢查並顯示該設備是否支持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;
}
}

clearVariables只需清除局部變量hasGsmCheckBox / isDualSimCheckBox ,而不是checkBox視圖本身。 要准備好顯示CheckBox,您必須像對onStart方法一樣使用checkBox.setChecked(true)

但是僅當活動不再可見時才調用onStart()

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

我猜您可能會嘗試將onStart()中的內容放入onResume()中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM