when is onRestoreInstanceState called?
Asked Answered
O

6

44

Sorry for my incomprehension, but I am new in the android development.

I have an application with activity A and activity B in it, and I go from activity A to activity B. When I left activity A, the onSaveInstanceState method was called, but when I went back to activity A (from activity B in the same application), the bundle in the onCreate method was null.

What can I do, to save the activity A's previous state? I only want to store the data for the application lifetime.

Can someone help me with this?

Here is my code for Activity A:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState != null)
    {
        Log.v("Main", savedInstanceState.getString("test"));
    }
    else
    {
        Log.v("Main", "old instance");
    }
}  

@Override
public void onSaveInstanceState(Bundle savedInstanceState) 
{
    Log.v("Main", "save instance");

    savedInstanceState.putString("test", "my test string");

    super.onSaveInstanceState(savedInstanceState);
}


public void buttonClick(View view)
{
    Intent intent = new Intent(this, Activity2.class);
    startActivity(intent);
}

Here is my code for Activity B, when I press a button to go back to activity A:

public void onBack(View view)
{
    NavUtils.navigateUpFromSameTask(this);
}
Octonary answered 12/3, 2013 at 13:25 Comment(4)
post your code and lets see how you are saving the bundle.Sirmons
check developer.android.com/reference/android/app/…Perrotta
possible duplicate of #6554817Ambi
I just posted my codeHoard
W
29

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.

Westernism answered 12/3, 2013 at 13:41 Comment(8)
1. I tried, nothing changed, 2. if Activity A might not be restarted, should I get this log message: Log.v("Main", "old instance") ?Hoard
What am I saying is that if onSaveInstanceState is called it does not guarantee that you will receive the bundle. onRestoreInstanceState will be called only if activity is was killed by the system previously.Westernism
Can you give me some tip please how can I save values from my activity (that would be persistent only in my application lifetime)Hoard
Just store the values as fields and add saving to onSaveInstanceState and restoring to onRestoreInstanceState and onCreate and you're done.Westernism
Updated the answer with the example of what I mean.Westernism
@Peter: Another way to say what Doctoror is saying is that, if you don't get a call to onRestoreInstanceState, you don't need it, because you haven't lost your state.Wenz
I found some misleading info in my answer and edited to be more correct.Westernism
Thank you so much for informing me about "Don't keep activities" It really helps me to debug the application better. Thank you for your help:)Emblazonry
S
59

To answer your question, have a look at the android doc: https://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState(android.os.Bundle)

It says that onRestoreInstanceState is called after onStart() method in the activity lifecycle.

Saline answered 4/10, 2014 at 8:5 Comment(1)
Thanks for answering the question asked in the title =)Wildee
W
29

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.

Westernism answered 12/3, 2013 at 13:41 Comment(8)
1. I tried, nothing changed, 2. if Activity A might not be restarted, should I get this log message: Log.v("Main", "old instance") ?Hoard
What am I saying is that if onSaveInstanceState is called it does not guarantee that you will receive the bundle. onRestoreInstanceState will be called only if activity is was killed by the system previously.Westernism
Can you give me some tip please how can I save values from my activity (that would be persistent only in my application lifetime)Hoard
Just store the values as fields and add saving to onSaveInstanceState and restoring to onRestoreInstanceState and onCreate and you're done.Westernism
Updated the answer with the example of what I mean.Westernism
@Peter: Another way to say what Doctoror is saying is that, if you don't get a call to onRestoreInstanceState, you don't need it, because you haven't lost your state.Wenz
I found some misleading info in my answer and edited to be more correct.Westernism
Thank you so much for informing me about "Don't keep activities" It really helps me to debug the application better. Thank you for your help:)Emblazonry
F
5

reference

onSaveInstanceState

... onPause()-> onSaveInstanceState() -> onStop() -> onDestory()

onRestoreInstanceState

onCreate()-> onStart()-> onRestoreInstanceState()-> onPostCreate(Bundle) ...

Or You can use LiveData. Save the states in it and observe.If the device rotates it'll update the views accordingly.

Fanechka answered 6/3, 2020 at 5:32 Comment(0)
H
3

This happens because you are navigating the hard way. If you used the phone's default back button navigation, your onCreate would get the bundle passed in.

To circumvent this issue, I suggest you save your state to shared preferences as well as a back up. When the bundle is null, restore it from the shared preferences.

Hereinto answered 2/5, 2013 at 15:26 Comment(0)
C
2

After onStart() which is after onCreate()

Cann answered 18/7, 2016 at 15:35 Comment(0)
T
0

I used in this case a flag and SharedPreferences. This should work, and when you change the screen orientation.

Thiosinamine answered 19/5, 2015 at 18:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.