I have an activity which starts various activities for result codes and on getting results in onActivityResult
method it starts appropriate activity based on result code.
onSaveInstanceState
is not getting called in Activity which started for result.
For example Navigation Activity starts Activity A as :
Intent intent = new Intent(this, A.class);
startActivityForResult(intent, Constants.REQUEST_CODE);
Then A finishes by setting result code so App will redirect to Navigation activity again and onActivityResult
method will called.
So my question is: Why Activity A's onSaveInstanceState
is not getting called at finish and navigation back to Navigation Activity ?
The method
onSaveInstanceState()
isn't called when anActivity
finishes naturally like on a back button press. That's your app itself destroying theActivity
. The method is only called if the Android OS anticipates that it may have to kill your activity to reclaim resources.If the
Activity
then actually gets killed by Android, the OS will make sure you receive a call toonRestoreInstanceState()
as well passing the same bundle you used to save your activity's state inonSaveInstanceState()
method.From the docs:
onSaveInstanceState() is only called if the Activity is being killed.
I don't know what exactly you want to do in that method, but you probably should move your code to the corresponding methods of the Activity Lifecycle.
from http://developer.android.com/reference/android/app/Activity.html :
Also the method description for onSaveInstanceState() describes exactly your situation:
I had the same issue happening to me on a "drawer menu" part of a "main activity", because I had overridden the "onSaveInstanceState" method in my main activity but had forgotten the call to super.onSaveInstanceState() (which, as a result, would never call the "onSaveInstanceState()" method of my "drawer menu" which was part of this main activity).
In other words: make sure you don't forget your calls to "super.onSaveInstanceState()" where necessary.