Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when is onRestoreInstanceState called?

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); } 
like image 237
Tünde Avatar asked Mar 12 '13 13:03

Tünde


People also ask

When on Save instance is called?

Configuration change scenario Note that onSaveInstanceState() is called when your activity goes into the background and NOT when the app process is about to be killed.

What is onSaveInstanceState () and onRestoreInstanceState () in activity?

The onSaveInstanceState() method allows you to add key/value pairs to the outState of the app. Then the onRestoreInstanceState() method will allow you to retrieve the value and set it back to the variable from which it was originally collected.

When onPause method is called in Android?

onPause. Called when the Activity is still partially visible, but the user is probably navigating away from your Activity entirely (in which case onStop will be called next). For example, when the user taps the Home button, the system calls onPause and onStop in quick succession on your Activity .

What's the purpose of onSaveInstanceState and when it is invoked in the activity lifecycle?

onSaveInstanceState() This method mainly used to keep the state in orientation change. when you rotate the screen activity will destroy and create again. So the data you attached in runtime will be lost. By saving them in the bundle object you can access them after the screen rotation.


2 Answers

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.

like image 151
Simon Avatar answered Oct 07 '22 22:10

Simon


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.

like image 32
Yaroslav Mytkalyk Avatar answered Oct 07 '22 20:10

Yaroslav Mytkalyk