从文档开始:
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() 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.
首先查看my post在保留的 fragment 上。它可能会有所帮助。
现在回答你的问题:
Does the fragment also retain its
viewstate, or will this be recreated on configuration change - what exactly is "retained"?
Fragment
的状态将在配置更改期间保留。具体来说,“保留”意味着 fragment 不会在配置更改时被销毁。也就是说,Fragment
即使配置更改导致底层 Activity
也会保留被摧毁。Will the fragment be destroyed when the user leaves the activity?
Activity
年代,Fragment
s 可能在内存资源不足时被系统销毁。您是否让 fragment 在配置更改时保留其实例状态不会影响系统是否会破坏 Fragment
s 一旦你离开 Activity
.如果您离开 Activity
(即按主页按钮),Fragment
s 可能会或可能不会被销毁。如果您离开 Activity
通过按下后退按钮(因此,调用 finish()
并有效地销毁 Activity
),所有 Activity
附上 Fragment
s 也会被销毁。Why doesn't it work with fragments on the back stack?
Activity
持有对 FragmentManager
的引用,以及 FragmentManager
管理后台堆栈。也就是说,无论您是否选择保留您的 Fragment
s 与否,Activity
(因此 FragmentManager
的后台堆栈)将在配置更改时被销毁。它可能不起作用的另一个原因是,如果允许保留 fragment 和非保留 fragment 都存在于同一个 backstack 上,事情可能会变得棘手。Which are the use cases where it makes sense to use this method?
Thread
实例的主机。或 AsyncTask
,管理其操作。见 my blog post有关此主题的更多信息。onConfigurationChanged
一样对待它。与 Activity
...不要仅仅因为您懒得正确实现/处理方向更改而将其用作创可贴。仅在需要时使用它。
关于android - 理解 Fragment 的 setRetainInstance(boolean),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11182180/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |