onDestroy question


Hi !
I have a question about onDestroy(…) and onSaveInstanceState(…). I know that when you change the orientation of the screen from normal to landscape, the current state is saved with onSaveInstanceState(…) (mCurrentIndex is saved to keep it simple) and it is restored to the previous state(the destroyed one) when the new interface is inflated. The question is like this: why when I click the Back button the OS doesn’t call onSaveInstanceState(…) like it does when you change the orientation ? If I am in the main activity and I click the Back button, to exit the app, then if I relaunch the application, I start again from the question 0. Does the OS think’s I am closing the app and it discards any saved instance ?
I’m using Android 4.4 on Nexus 7.


I think I figured it out. Being the last activity from this application(process) on the Back Stack inside the ActivityManager, when the user clicks the Back button, the last activity pops off the stack and so the OS assumes that the user wants to close the app and it doesn’t bother himself to save the instance bundle. Good luck :smiley:


Also from documentation [quote]Note: There’s no guarantee that onSaveInstanceState() will be called before your activity is destroyed, because there are cases in which it won’t be necessary to save the state ([color=#FF8000]such as when the user leaves your activity using the Back button, because the user is explicitly closing the activity[/color]). If the system calls onSaveInstanceState(), it does so before onStop() and possibly before onPause().[/quote]