向上导航和保存的实例数据 [英] Up navigation and saved instance data

查看:95
本文介绍了向上导航和保存的实例数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个应用程序有2个活动,A和B.

An app has 2 activities, A and B.

A的实例数据已保存

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("foo", 0);
}

A有

int bar;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ...
    if (savedInstanceState != null) {
        bar = savedInstanceState.getInt("foo");
    } else {
        bar = -1;
    }
}

还原数据.

活动B启用了操作栏,并且

Activity B has the actionbar enabled and

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    // ...
}

启用向上导航.在AndroidManifest.xml中,A也被列为B的父级活动.

to enable up navigation. Also A is listed as the parent activity of B in AndroidManifest.xml.

当用户从A导航到B时,将调用onSaveInstanceState,并且如果他们使用后退按钮活动A导航回A,则会正确地恢复其保存的信息.

When a user navigates from A to B onSaveInstanceState is called and if they navigate back to A using the back button activity A correctly restores it's saved information.

但是,当用户从A导航到B时,调用onSaveInstanceState,然后使用向上导航返回到A,即使保存了信息,也会通过null传递.

However when the user navigates from A to B onSaveInstanceState is called and then up navigation is used to return to A the onCreate(Bundle savedInstanceState) is passed null even though information was saved.

如何启动导航以传递在onSaveInstanceState中创建的捆绑包?

How do I get up navigation to pass the the Bundle created in onSaveInstanceState?

推荐答案

我遇到了同样的问题-我只想进行两个活动A和B,其中A是B的父母.当您在B中时,请单击向上按钮,返回到A,然后在onCreate()中从捆绑包中恢复数据.但是,捆绑包始终为null.

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.

在大多数情况下,人们建议为清单中的活动设置android:launchMode="singleTop".实际上,这不一定是所需的行为,因为这仅意味着当您单击向上"按钮时,将不会重新创建父级.您可能会发现这很好,但就我而言,我绝对希望重新创建父级以反映数据库中的某些更改.

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.

据说:

当您按回时,活动将被销毁,并且其拥有的数据将丢失.此后,当您重新打开应用程序时,您将始终在捆绑包中得到null,因为正在创建一个新的新实例.

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.

由于某些更改(例如旋转)或由于其在后台暂停了很长时间而重新创建活动时,将使用Bundle savedInstanceState包.

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.

如果您要保留一些数据,请为小型内容考虑SharedPreferences,或者为大型内容考虑数据库(SQLite,Realm)或文件.

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

我猜这个捆绑包的方法根本不是Android期望我们这样做的方式.您应该使用SharedPreferences对象并将foo变量保存在其中.

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.

这篇关于向上导航和保存的实例数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆