Fragments 中的 `onViewStateRestored` 如何工作? [英] How does `onViewStateRestored` from Fragments work?

查看:42
本文介绍了Fragments 中的 `onViewStateRestored` 如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的对 Fragment 的内部状态感到困惑.我有一个 Activity 一次只保存一个 Fragment 并替换它,如果应该显示另一个 Fragment.从文档 onSaveInstanceState 被称为 ONLY 如果活动 onSaveInstanceState 被调用(在我的情况下没有被调用).

I am really confused with the internal state of a Fragment. I have an Activity holding only one Fragment at once and replaces it, if another Fragment should get shown. From the docs onSaveInstanceState is called ONLY if the Activitys onSaveInstanceState is getting called (which isn't called in my case).

如果我停止我的 Fragment,我会自己将它的状态存储在一个 Singleton 中(是的,我知道我也讨厌 Singleton,但这不是我的主意).所以我必须重新创建整个ViewHirarchy,创建新的视图(通过使用关键字new),恢复其状态并在onCreateView 中返回它们.我在这个视图中还有一个复选框,我明确地希望从中存储它的状态.

If I stop my Fragment, I'll store its state myself inside a Singleton (yeah, I know I hate Singletons, too, but wasn't my idea to do so). So I have to recreate the whole ViewHirarchy, create new Views (by using the keyword new), restore its state and return them in onCreateView. I also have a Checkbox inside this View from which I explicitly do NOT want to store its state.

然而,FragmentManager 想要智能"并使用我自己从未创建的 Bundle 调用 onViewStateRestored,并恢复"旧 CheckBox 的状态 并将其应用于我的 NEW CheckBox.这引发了很多问题:

However the FragmentManager wants to be "intelligent" and calls onViewStateRestored with a Bundle I never created myself, and "restores" the state of the old CheckBox and applies it to my NEW CheckBox. This throws up so many questions:

我可以从 onViewStateRestored 控制包吗?

Can I control the bundle from onViewStateRestored?

FragmentManager 如何获取(可能是垃圾收集的)CheckBox 的状态并将其应用于新的?

How does the FragmentManager take the state of a (probably garbage-collected) CheckBox and applies it to the new one?

为什么它只保存 Checkbox 的状态(而不是 TextViews 的状态??)

Why does it only save the state of the Checkbox (Not of TextViews??)

总结一下:onViewStateRestored 是如何工作的?

So to sum it up: How does onViewStateRestored work?

注意我使用的是 Fragmentv4,所以 onViewStateRestored

Note I'm using Fragmentv4, so no API > 17 required for onViewStateRestored

推荐答案

好吧,有时片段会让人有点困惑,但过一段时间你就会习惯它们,并知道它们毕竟是你的朋友.

Well, sometimes fragments can get a little confusing, but after a while you will get used to them, and learn that they are your friends after all.

>

如果在片段的 onCreate() 方法上,您执行以下操作: setRetainInstance(true);视图的可见状态将被保留,否则不会.

If on the onCreate() method of your fragment, you do: setRetainInstance(true); The visible state of your views will be kept, otherwise it won't.

假设类 F 的一个名为f"的片段,它的生命周期是这样的:- 在实例化/附加/显示它时,按以下顺序调用 f 的方法:

Suppose a fragment called "f" of class F, its lifecycle would go like this: - When instantiating/attaching/showing it, those are the f's methods that are called, in this order:

F.newInstance();
F();
F.onCreate();
F.onCreateView();
F.onViewStateRestored;
F.onResume();

此时,您的片段将在屏幕上可见.假设设备是旋转的,因此必须保留分片信息,这是旋转触发的事件流:

At this point, your fragment will be visible on the screen. Assume, that the device is rotated, therefore, the fragment information must be preserved, this is the flow of events triggered by the rotation:

F.onSaveInstanceState(); //save your info, before the fragment is destroyed, HERE YOU CAN CONTROL THE SAVED BUNDLE, CHECK EXAMPLE BELLOW.
F.onDestroyView(); //destroy any extra allocations your have made
//here starts f's restore process
F.onCreateView(); //f's view will be recreated
F.onViewStateRestored(); //load your info and restore the state of f's view
F.onResume(); //this method is called when your fragment is restoring its focus, sometimes you will need to insert some code here.


//store the information using the correct types, according to your variables.
@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable("foo", this.foo);
    outState.putBoolean("bar", true);
}

@Override
public void onViewStateRestored(Bundle inState) {
    super.onViewStateRestored(inState);
    if(inState!=null) {
        if (inState.getBoolean("bar", false)) {
            this.foo = (ArrayList<HashMap<String, Double>>) inState.getSerializable("foo");
        }
    }

}

这篇关于Fragments 中的 `onViewStateRestored` 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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