理解 Fragment 的 setRetainInstance(boolean) [英] Understanding Fragment's setRetainInstance(boolean)
问题描述
从文档开始:
public void setRetainInstance(布尔保留)
public void setRetainInstance (boolean retain)
控制是否在 Activity 重新创建期间保留片段实例(例如来自配置更改).这只能用于不在后堆栈中的片段.如果设置,则重新创建 Activity 时片段生命周期会略有不同:
Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change). This can only be used with fragments not in the back stack. If set, the fragment lifecycle will be slightly different when an activity is recreated:
- onDestroy() 不会被调用(但 onDetach() 仍然会被调用,因为片段正在与其当前活动分离).
- onCreate(Bundle) 不会被调用,因为片段没有被重新创建.
- onAttach(Activity) 和 onActivityCreated(Bundle) 仍会被调用.
我有一些问题:
片段是否也保留其视图,还是会在配置更改时重新创建?保留"究竟是什么?是什么意思?
Does the fragment also retain its view, or will this be recreated on configuration change? What exactly does "retained" mean?
当用户离开活动时片段会被销毁吗?
Will the fragment be destroyed when the user leaves the activity?
为什么它不适用于后端堆栈上的片段?
Why doesn't it work with fragments on the back stack?
在哪些用例中使用这种方法是有意义的?
Which are the use cases where it makes sense to use this method?
推荐答案
首先查看 我关于保留片段的帖子.可能会有帮助.
First of all, check out my post on retained Fragments. It might help.
现在回答您的问题:
片段是否也保留其 view 状态,还是会在配置更改时重新创建 - 究竟什么是保留"?
Does the fragment also retain its
viewstate, or will this be recreated on configuration change - what exactly is "retained"?
是的,Fragment
的状态将在配置更改期间保留.具体来说,保留"意味着片段将不会在配置更改时被破坏.也就是说,即使配置更改导致底层 Activity
被销毁,Fragment
也会保留.
Yes, the Fragment
's state will be retained across the configuration change. Specifically, "retained" means that the fragment will not be destroyed on configuration changes. That is, the Fragment
will be retained even if the configuration change causes the underlying Activity
to be destroyed.
当用户离开活动时片段会被销毁吗?
Will the fragment be destroyed when the user leaves the activity?
和Activity
s一样,Fragment
s在内存资源不足时可能会被系统销毁.您是否让片段在配置更改时保持其实例状态不会影响系统是否会在您离开 Activity
后销毁 Fragment
.如果您离开 Activity
(即通过按下主页按钮),Fragment
可能会或可能不会被销毁.如果您通过按后退按钮离开 Activity
(因此,调用 finish()
并有效地销毁 Activity
),所有 >Activity
附加的Fragment
也将被销毁.
Just like Activity
s, Fragment
s may be destroyed by the system when memory resources are low. Whether you have your fragments retain their instance state across configuration changes will have no effect on whether or not the system will destroy the Fragment
s once you leave the Activity
. If you leave the Activity
(i.e. by pressing the home button), the Fragment
s may or may not be destroyed. If you leave the Activity
by pressing the back button (thus, calling finish()
and effectively destroying the Activity
), all of the Activity
s attached Fragment
s will also be destroyed.
为什么它不适用于后端堆栈上的片段?
Why doesn't it work with fragments on the back stack?
不支持它的原因可能有多种,但对我来说最明显的原因是 Activity
持有对 FragmentManager
的引用,而 FragmentManager
管理后台堆栈.也就是说,无论您是否选择保留 Fragment
s,Activity
(以及 FragmentManager
的后台堆栈)都将是在配置更改时销毁.它可能不起作用的另一个原因是,如果允许保留片段和非保留片段存在于同一个 backstack 中,事情可能会变得棘手.
There are probably multiple reasons why it's not supported, but the most obvious reason to me is that the Activity
holds a reference to the FragmentManager
, and the FragmentManager
manages the backstack. That is, no matter if you choose to retain your Fragment
s or not, the Activity
(and thus the FragmentManager
's backstack) will be destroyed on a configuration change. Another reason why it might not work is because things might get tricky if both retained fragments and non-retained fragments were allowed to exist on the same backstack.
在哪些用例中使用这种方法是有意义的?
Which are the use cases where it makes sense to use this method?
保留的片段对于跨活动实例传播状态信息(尤其是线程管理)非常有用.例如,片段可以作为 Thread
或 AsyncTask
实例的宿主,管理其操作.请参阅我的博文 有关此主题的更多信息.
Retained fragments can be quite useful for propagating state information — especially thread management — across activity instances. For example, a fragment can serve as a host for an instance of Thread
or AsyncTask
, managing its operation. See my blog post on this topic for more information.
一般来说,我会像使用 onConfigurationChanged
和 Activity
一样对待它......不要仅仅因为你懒得实施而将它用作创可贴/正确处理方向更改.仅在需要时使用它.
In general, I would treat it similarly to using onConfigurationChanged
with an Activity
... don't use it as a bandaid just because you are too lazy to implement/handle an orientation change correctly. Only use it when you need to.
这篇关于理解 Fragment 的 setRetainInstance(boolean)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!