when is onRestoreInstanceState called?

Tünde picture Tünde · Mar 12, 2013 · Viewed 38.5k times · Source

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);
}

Answer

Simon picture Simon · Oct 4, 2014

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.