FragmentActivity onSaveInstanceState 未被调用 [英] FragmentActivity onSaveInstanceState not getting called

查看:29
本文介绍了FragmentActivity onSaveInstanceState 未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一些关于 onSaveInstanceState 的类似问题没有被 Fragment 调用,但在我的情况下 Fragment 的工作正常,它是遇到问题的主要 FragmentActivity.

I have seen a few similar questions about onSaveInstanceState not getting called for Fragments, but in my case Fragments work fine, it's the main FragmentActivity that's having trouble.

相关代码看起来相当简单:

The relevant code looks fairly simple:

public class MyFActivity extends FragmentActivity implements ActionBar.TabListener { 
    String[] allValues; // data to save

    @Override
    protected void onSaveInstanceState (Bundle outState) {
        Log.d("putting it!", allValues.toString());
        outState.putStringArray("allValues", allValues);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            allValues = savedInstanceState.getStringArray("allValues");
            Log.d("getting it!", allValues.toString());
        }
    }
}

暂停活动时(使用后退按钮),onSaveInstanceState 永远不会被调用,因此,savedInstanceState 在恢复应用程序时的 onCreate 方法.我尝试添加这样的块:

When pausing the activity (using the back button), the onSaveInstanceState is never called, and consequently, savedInstanceState is always null within the onCreate method upon resuming the app. I tried adding a block like this:

@Override
public void onPause() {
    super.onPause();
    onSaveInstanceState(new Bundle());      
}

这是在 https://stackoverflow.com/a/14195202/362657 中提出的,但是 onSaveInstanceState 然后被调用, savedInstanceStateonCreate 方法中保持 null .我错过了什么?

which was suggested in https://stackoverflow.com/a/14195202/362657 but while onSaveInstanceState then gets called, savedInstanceState remains null within onCreate method. What am I missing?

推荐答案

这里的问题是您误解了 onSaveInstanceState 的工作原理.它旨在保存 Activity/Fragment 的状态,以防操作系统因内存原因或配置更改而需要销毁它.当 Activity/Fragment 返回/重新启动时,此状态会在 onCreate 中传回.

The issue here is that you are misunderstanding how onSaveInstanceState works. It is designed to save the state of the Activity/Fragment in the case that the OS needs to destroy it for memory reasons or configuration changes. This state is then passed back in onCreate when the Activity/Fragment is returned to / restarted.

Fragment 中,它们所有的生命周期回调都直接绑定到它们的父 Activity.因此,当 Fragment 的父 Activity 调用了 onSaveInstanceState 时,onSaveInstanceState 会在 Fragment 上被调用.

In a Fragment, all of their lifecycle callbacks are directly tied to their parent Activity. So onSaveInstanceState gets called on the Fragment when its parent Activity has onSaveInstanceState called.

暂停活动时(使用后退按钮),从不调用 onSaveInstanceState,因此,在恢复应用程序时,onCreate 方法中的 savedInstanceState 始终为 null.

When pausing the activity (using the back button), the onSaveInstanceState is never called, and consequently, savedInstanceState is always null within the onCreate method upon resuming the app.

按下回键时,用户正在销毁 Activity 及其子 Fragment,因此没有理由调用 onSaveInstanceState,因为实例正在被销毁.当你重新打开 Activity 时,它是一个全新的实例,没有保存的状态,所以 onCreate 中传入的 Bundlenull.这完全符合设计.但是,尝试旋转设备或点击主页按钮,然后您将看到 Activity 及其子 Fragment 调用了 onSaveInstanceState 并通过返回时返回onCreate.

When pressing back, the user is destroying the Activity, and therefore its children Fragments, so there is no reason to call onSaveInstanceState, since the instance is being destroyed. When you reopen the Activity, it's a brand new instance, with no saved state, so the Bundle passed in onCreate is null. This is behaving exactly as designed. However, try rotating the device or hitting the home button, then you will see the Activity and its children Fragments have onSaveInstanceState called, and passed back in onCreate when returned to.

您添加的hack,直接在onPause 内部调用onSaveInstanceState(new Bundle());,是一种非常糟糕的做法,因为您应该永远直接调用生命周期回调.这样做会使您的应用程序进入非法状态.

The hack you added, directly calling onSaveInstanceState(new Bundle()); inside of onPause, is a very bad practice, as you should never call the lifecycle callbacks directly. Doing so can put your app into illegal states.

如果您真正想要的是让您的数据在应用程序实例之外持久保存,我建议您考虑使用 SharedPreferences数据库更高级的数据.然后,您可以将持久数据保存在 onPause() 中或在它发生变化时保存.

If what you really want is for your data to persist beyond an instance of your app, I suggest you look into using SharedPreferences or databases for more advanced data. You can then save your persistent data in onPause() or whenever it changes.

希望这会有所帮助.

这篇关于FragmentActivity onSaveInstanceState 未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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