Search code examples
androidandroid-lifecycleactivity-lifecycleonsaveinstancestate

When onRestoreInstanceState is not called?


Docs describes when onRestoreInstanceState IS called:

This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. (...) This method is called between onStart() and onPostCreate(Bundle). This method is called only when recreating an activity; the method isn't invoked if onStart() is called for any other reason.

I'm super curious what does "onStart() is called for any other reason" mean in context of onRestoreInstanceState?


Solution

  • 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.