Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

android - 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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...