Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

android - onSaveInstanceState is not saving my values ( onCreate input Bundle is always null )

Saving bundle (activity A):

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("test", "value");
    super.onSaveInstanceState(outState);
}

Navigating to activity B;

startActivity(new Intent(getBaseContext(), B.class));

Going back to activity A:

startActivity(new Intent(getBaseContext(), A.class));

Trying to load the value I wrote to bundle in activity A:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

Log.d("MY", "saved instance is null"+ Boolean.toString(savedInstanceState == null));
}

Returns always savedInstanceState = null. What I'm missing here?

onRestoreInstanceState is never triggered when I return to main activity

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

I got this solve by having:

intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

when re-launching the old intent that has the saved state from background. Without this flag, I think that when startActivity is called, it creates a new instance of the Activity instead of getting the old one from the stack.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...