nmxprime
nmxprime

Reputation: 1506

Android preserve variable state during minimize and re launch

i have a simple activity where i start a service in onCreate depending on checkbox state.

@Override
protected void onCreate(Bundle savedInstanceState) {
    if(savedInstanceState == null)
        Log.e(TAG,"savedInstanceState is null");
    if(savedInstanceState != null){
        Log.e(TAG,"savedInstanceState is not null");
    }
    super.onCreate(savedInstanceState);

on checkbox state checked starting the service and setting serviceStarted

toggleButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {



        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            if(isChecked){

                Log.e(TAG,"starting service");
                serviceStarted = true;
                startService(new Intent(getApplicationContext(), MyService.class));

            }else{

                serviceStarted = false;
                stopService(new Intent(getApplicationContext(), MyService.class));


            }

        }
    });

i have overrided below functions

@Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    Log.e(TAG,"onSaveInstanceState called");
    super.onSaveInstanceState(outState);
}
@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Log.e(TAG,"onResume called : serviceStarted = "+String.valueOf(serviceStarted));
}
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    Log.e(TAG,"onPause called : serviceStarted = "+String.valueOf(serviceStarted));
}

Now in my logcat, on minimize and re-launching the app after starting service, i see below

04-22 12:34:42.150: E/MainActivity(19720): onPause called : serviceStarted = true
04-22 12:34:44.700: E/MainActivity(19720): onResume called : serviceStarted = false

Now my need is

I want to preserve state of serviceStarted without using sharedpreferences

Anyway there, or should i go with service binding model ? Please help Thank you

Upvotes: 1

Views: 470

Answers (1)

Ivan Bartsov
Ivan Bartsov

Reputation: 21066

Well, just use the whole save instance state thing, that's what it's for:

protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean("serviceStarted", mServiceStarted);
}

protected void onRestoreInstanceState(Bundle state) {
    super.onRestoreInstanceState(state);
    mServiceStarted = state.getBoolean("serviceStarted", false);
}

But actually, if your activity gets destroyed for reals (by back button or by the OS), you'll still lose state, so the more reliable option is to use SharedPreferences.

But in your case, service can still get killed between the times your activity pauses and gets brought back -- then your serviceStarted will be obsolete. So just use a static member to know the state:

public MyService extends Service {
    private static volatile boolean mIsRunning = false;
    public void onCreate() {
        MyService.mIsRunning = true;
    }
    public void onDestroy() {
        MyService.mIsRunning = false;
    }
    public static boolean isRunning() {
        return mIsRunning;
    }
}

Upvotes: 2

Related Questions