FragmentActivity的onSaveInstanceState没有得到所谓的 [英] FragmentActivity onSaveInstanceState not getting called

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

问题描述

我看到约了几个类似的问题的onSaveInstanceState 没有得到要求片段 S,但对我来说片段的工作很好,这是主要的 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.

相关code看起来相当简单:

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());      
}

这是建议在 http://stackoverflow.com/a/14195202/362657 但同时的onSaveInstanceState 再被调用, savedInstanceState 仍然的onCreate 方法。我在想什么?

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

推荐答案

这里的问题是,你是误会了如何的onSaveInstanceState 的作品。它的目的是保存活动 / 片段,该操作系统需要摧毁它的内存的情况下的状态原因或配置更改。这个状态然后被传递回的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.

片段,所有的生命周期回调都直接关系到他们的父母活动。因此,的onSaveInstanceState 被调用的片段时,其父活动已经的onSaveInstanceState 调用。

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不会被调用,因此,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.

在pressing回来,用户正在摧毁活动,因此,其子女片段 S,所以没有理由骂的onSaveInstanceState ,因为该实例被销毁。当重新打开活动,这是一个全新的实例,没有保存的状态,因此捆绑通过了的onCreate 。这是行为完全一样的设计。然而,尝试旋转设备或击中主页按钮,然后你会看到活动及其子片段活动的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.

破解您加入,直接调用的onSaveInstanceState(新包()); 里面的的onPause ,是一个非常不好的做法,因为你应该永远直接调用生命周期回调。这样做可以把你的应用程序变成非法状态。

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.

如果你真正想要的是你的数据后仍然保持你的应用程序的一个实例,我建议你考虑使用<一个href="http://developer.android.com/reference/android/content/Shared$p$pferences.html"><$c$c>Shared$p$pferences数据库获取更高级的数据。然后,您可以保存的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天全站免登陆