了解片段的setRetainInstance(布尔) [英] Understanding Fragment's setRetainInstance(boolean)

查看:144
本文介绍了了解片段的setRetainInstance(布尔)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从开始的文档:

公共无效setRetainInstance(布尔保留)

public void setRetainInstance (boolean retain)

控制跨越活动再创造一个片段实例是否被保留(例如,从一个配置变化)。这只能用于与碎片不会在后面栈。如果设置,该片段的生命周期会略有不同,当一个活动被重建:

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(捆绑)将不会被调用,因为没有被重新创建的片段。
  •   
  • onAttach(活动)和onActivityCreated(束)仍将被调用。
  •   
  • onDestroy() will not be called (but onDetach() still will be, because the fragment is being detached from its current activity).
  • onCreate(Bundle) will not be called since the fragment is not being re-created.
  • onAttach(Activity) and onActivityCreated(Bundle) will still be called.

我有一些问题:

  • 请问片段还保留其观点,或许这会在配置更改重现?究竟什么是保留?

  • Does the fragment also retain its view, or will this be recreated on configuration change? What exactly is "retained"?

将在用户离开活动时,片段被破坏?

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?

推荐答案

首先,检查出<一href="http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html">my交 留存片段。它可能的帮助。 :)

First of all, check out my post on retained Fragments. It might help. :)

现在回答你的问题...

Now to answer your questions...

请问片段还保留其视图的状态,或许这会在配置更改重建? - 究竟是保留

Does the fragment also retain its view state, or will this be recreated on configuration change - what exactly is "retained"?

是的,片段的状态将整个配置更改保留。具体而言,保留是指该片段会的没有的是对配置变化销毁。也就是说,片段保留即使配置变化导致潜在的活动被消除了。

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?

就像活动 S,片段之间可以由系统破坏,当内存资源不足。无论你有你的片段保留在整个配置更改自己的实例状态都会对与否,系统没有任何影响会破坏片段一度离开了活动。如果将活动(即由pressing home键),在片段 S可以或不可以被破坏。如果将活动由pressing后退按钮(因此,调用完成(),有效地破坏活动),所有的活动第附着片段取值也将被销毁。

Just like Activitys, Fragments 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 Fragments once you leave the Activity. If you leave the Activity (i.e. by pressing the home button), the Fragments 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 Activitys attached Fragments will also be destroyed.

为什么它不与碎片的背叠工作?

Why doesn't it work with fragments on the back stack?

有为什么它不支持可能是多种原因,但最明显的原因让我看到了活动持有引用 FragmentManager FragmentManager 管理​​backstack。也就是说,没有如果你选择保留片段 s或没有,活动(因而<$事项C $ C> FragmentManager 的backstack)将在配置更改破坏。另一个原因可能无法正常工作是因为事情可能变得棘手,如果既保留碎片的的无保留片段被允许存在于同一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 Fragments 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?

保留片段可以成为传播状态信息是非常有用 - 尤其是线程管理 - 整个活动实例。例如,一个片段可以作为主机的发一个实例的AsyncTask ,管理其操作。请参阅<一href="http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html">my博客文章 关于此主题的更多信息。

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 活动 ...不要使用它作为一个创可贴只是因为你懒得实施/正确处理的方向变化。只使用它时,你需要

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.

这篇关于了解片段的setRetainInstance(布尔)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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