Up navigation and saved instance data

北慕城南 提交于 2019-12-04 03:43:08

I faced the same issue - I simply wanted to have two activities A and B, where A is the parent of B. When you are in B and click the Up button, you return to A and in onCreate() you restore your data from the Bundle. However, the bundle is always null.

In most cases, people recommend setting android:launchMode="singleTop" for the activity in the manifest. This is, actually, not necessarily a desired behaviour, because it simply means that when you click the Up button, the parent will not be recreated. You might find this OK, but in my case I definitely wanted to have the parent recreated to mirror some changes in the DB.

Other solutions say to attach a listener to the Up button and add this code:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent intent = NavUtils.getParentActivityIntent(this);
            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            NavUtils.navigateUpFromSameTask(this);
            break;
    }

    return super.onOptionsItemSelected(item);
}

This achieved no effect for me. I don't actually understand what this code is supposed to do.

Still, I think I found an answer to the question in another post. Check it here.

It's said:

When you press back, the Activity is destroyed and the data it had is lost. After this you will always get null in the bundle as a new, fresh instance is being created when you reopen the app.

The Bundle savedInstanceState is used when the activity is being recreated due to some changes (like rotation), or because it was paused in the background for a long time.

If you want to persist some data, consider SharedPreferences for small stuff, or maybe a database (SQLite, Realm) or files for large stuff.

I guess that the approach with the bundle is simply not the way Android expects us to do it. You should use a SharedPreferences object and save your foo variable inside it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!