Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onSaveInstanceState is not getting called

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 ?

like image 976
HemangNirmal Avatar asked Feb 12 '23 01:02

HemangNirmal


1 Answers

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 :

Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation.

Also the method description for onSaveInstanceState() describes exactly your situation:

Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when an activity is being placed in the background or on its way to destruction, or onStop() which is called before destruction. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact.

like image 109
Yalla T. Avatar answered Feb 15 '23 09:02

Yalla T.