0

In order to see if onRestoreInstanceState is called , i have made this little java code

package com.MCHAppy.demostate.app;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;


public class MainActivity extends ActionBarActivity {
    private int visiters=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

     }

     @Override
     protected void onResume() {
         super.onResume();
          visiters++;
         Log.d("Test","Resumed");
     }

     @Override
       protected void onSaveInstanceState(Bundle outState) {
           super.onSaveInstanceState(outState);
           outState.putInt("visiters",visiters);
           Log.d("Test",visiters+" visiters  was saved ");
       }

     @Override
     protected void onRestoreInstanceState(Bundle savedInstanceState) {
           super.onRestoreInstanceState(savedInstanceState);
           visiters=savedInstanceState.getInt("visiters");
           Log.d("Test",visiters+" visiters was restored");
     }
 }

When i click the home icon I got this in the Logcat

  06-09 17:09:58.240    1316-1316/com.MCHAppy.demostate.app D/MCHAppy﹕ 1 visiters  was saved

And when i turn back to my activity I got this in the Logcat

06-09 17:58:13.230    1316-1316/com.MCHAppy.demostate.app D/MCHAppy﹕ Resumed

It seems that onRestoreInstanceState has never been called . Is that really what hapened ?

Thanks

1 Answers1

0

via the documentation

This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState.

The key part of this is that it is only called when the activity is being re-initialized. However, the same bundle is always passed in onCreate(Bundle savedInstanceState) so you can move your logic there.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        visiters=savedInstanceState.getInt("visiters");
    }
}
James McCracken
  • 15,488
  • 5
  • 54
  • 62
  • But onCreate method is called just the first time you launch the activity. So when i click on home icon and then get back to my app , onCreate will not be called and i can't see the number of visiters . –  Jun 09 '14 at 22:16
  • 1
    That is not correct. Both onCreate and onRestoreInstanceState will be called every time the activity is being re-initialized. If onCreate does not get called, then you were never destroyed and `visiters` will still have its previous value. [This article](http://developer.android.com/training/basics/activity-lifecycle/recreating.html) goes into further detail. – James McCracken Jun 09 '14 at 22:20
  • 1
    To be sure , I have putted a `Log.d("Test","onCreate method was called to restore state");` inside the onCreate method But Logcat show that message the first time then it has never shown me that again . what that means ? –  Jun 09 '14 at 22:35
  • It means your activity is never being destroyed in the first place. Therefore it never needs to save and restore its instance. Put a log statement inside of `onDestroy(...)` to see if it is ever called. – James McCracken Jun 09 '14 at 22:37
  • onDestroy was never called . According to the article that you have mentioned , oncreate --> onRestoreInstanceState --> onResume . Can't be called like this onPause --> onStop --> onRestart --> onStart --> onRestoreInstanceState --> onResume ? –  Jun 09 '14 at 22:56
  • From the e article `Instead of restoring the state during onCreate() you may choose to implement onRestoreInstanceState(), which the system calls after the onStart() method.` .. Am i wrong ???? –  Jun 09 '14 at 23:00
  • The activity is never being destroyed, therefore it never needs to be recreated, therefore `onRestoreInstanceState()` is never called. If `visiters` had a value of `1` previously, it will still have that same value without any saving/restoring of state. – James McCracken Jun 09 '14 at 23:09
  • I'm not sure that's true if the app has been sent to the background. If it's opened again via the task manager it should still have the state, if it's opened via the launcher icon it should be a new instance of the activity. – Dave Morrissey Jun 09 '14 at 23:14
  • According to you , onRestoreInstanceState is called only if the activity was destroyed . But according to the official documentation , it may be called after the onStart method . So there is two possible lifecycle : oncreate --> onRestoreInstanceState and onStart --> onRestoreInstanceState . Am i wrong ?? –  Jun 09 '14 at 23:19
  • That's the way I believe it happens too. Why is there such a learning curve on onSaveInstanceState onRestoreInstanceState with android developers is what I'd like to know? – danny117 Jun 10 '14 at 02:13
  • Note that I'm not disputing the order in which the methods are called. I am saying that onRestoreInstanceState will not even be called if the activity is being created for the first time. It will only be called if the activity has previously been destroyed and is being recreated. – James McCracken Jun 10 '14 at 16:07