Janos
Janos

Reputation: 720

Android Activity Life Cycle --- how to reset variables upon restarting an Activity?

I will try to keep my question brief as possible:

I have this simple Activity, which is used to check and show whether the device is GSM capable and has dual SIM or not. My problem is: -When app is exited, then the 2nd SIM is turned on and the app is restarted, the checkbox shows it is still not ready. The same thing when started with active 2nd card, and being turned off. -When the app is killed in task manager, and started again it shows the correct state of SIM the first time, but when exiting with back key, the same problem occurs like mentioned in the previous point. -I clear all the variables of this Activity in all callback functions for all stages of the Android app lifecycle, but the problem still remains.

What am I doing wrong, or do not count with. Thanks for your precious help in advance

Jani

Hereby the code of the 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;
}
}

Upvotes: 1

Views: 949

Answers (1)

Tamaki Sakura
Tamaki Sakura

Reputation: 519

clearVariables just clear the local variables hasGsmCheckBox/isDualSimCheckBox, which is not the checkBox view itself. To make the CheckBox shown ready you have to use checkBox.setChecked(true), like what you did on the onStart method;

But onStart() is only called when the activity is no longer visible

See https://i.sstatic.net/YvmA8.png

I guess you could probably try to put what's in onStart() into in onResume().

Upvotes: 2

Related Questions