When Exactly Are Onsaveinstancestate() and Onrestoreinstancestate() Called

When exactly are onSaveInstanceState() and onRestoreInstanceState() called?

Per the documentation:

void onRestoreInstanceState (Bundle savedInstanceState)

This method is called between onStart() and onPostCreate(Bundle).

void onSaveInstanceState (Bundle outState)

If called, this method will occur after onStop() for applications targeting platforms starting with Build.VERSION_CODES.P. For applications targeting earlier platform versions this method will occur before onStop() and there are no guarantees about whether it will occur before or after onPause().

when is onRestoreInstanceState called?

Saving and restoring state is meant to save the current temporary data that is obsolete when user exits the application.
When you minimize or leave the Activity by opening next one it might be killed by the system due to lack of resources and restarted with savedInstanceState when you get back to it. So use onSaveInstanceState() only for saving minimize-restore session data or data that should be preserved on rotation.

So if you start a new Activity in front and get back to the previous one (what you are trying to do), the Activity A might not be killed (just stopped) and restarted without going being destroyed. You can force killing it and restoring by checking Don't keep activities in developer options menu.

If you call finish() or remove the Activity from recent task list the savedInstanceState will not be passed to onCreate() since the task was cleared.

If the value must be persistent consider using SharedPreferences.

When onRestoreInstanceState is not called?

The normal lifecycle of an Activity looks like this:

  • onCreate()
  • .
  • onStart()
  • onResume()
  • onPause()
  • onStop()
  • .
  • onDestroy()

The lifecycle callbacks between onStart() and onStop() can occur over and over again if the Activity is completely obscured by another Activity. In this case, onStop() is called when the Activity is completely obscured by another Activity. When the Activity is made visible again, onStart() will be called without onRestoreInstanceState() being called because the Activity is not being recreated.

Why onRestoreInstanceState() never gets called

Well, if onRestart() is called, the value of the instance variables would be maintained by the application stack itself and thus you do not need to restore them.

onCreate() method is only called when your Activity's onStop() is called and the process is killed.

Please refer the Activity life cycle Android Activity Life Cycle for a clear understanding.

You may want to check whether the onStop() method is called and if your process is killed. I do no think that your process gets killed by the scenario which you have described.

the onRestoreInstanceState() method is very tricky. I do not know when exactly it is called but I saw it was called once while changing from Potrait to Landscape.

onSaveInstanceState() and onRestoreInstanceState(Parcelable state) are not called?

After digging in android os I have finally figured it out. As I suspected: there is nothing wrong with those two methods. They are just not called.
On the Web you can read that 'onRestoreInsatnceState is called when activity is re-created' Ok, it make sens but it's not completely true. Yes, onRestoreInstanceState() is called when activity is recreated but only iff:

it was killed by the OS. "Such situation happen when:

  • orientation of the device changes (your activity is destroyed and recreated)
  • there is another activity in front of yours and at some point the OS kills your activity in order to free memory (for example). Next time when you start your activity onRestoreInstanceState() will be called."

So if you are in your activity and you hit Back button on the device, your activity is finish()ed and next time you start your app it is started again (it sounds like re-created, isn't?) but this time without saved state because you intentionally exited it when you hit Back button.

Related Topics

Leave a reply