Android 为什么要使用 Fragment#setRetainInstance(boolean)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11160412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why use Fragment#setRetainInstance(boolean)?
提问by H?vard Geithus
I find Fragment#setRetainInstance(true) confusing. Here is the Javadoc, extracted from the Android Developer API:
我发现 Fragment#setRetainInstance(true) 令人困惑。这是从Android Developer API 中提取的 Javadoc :
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) willstill be called.
public void setRetainInstance(布尔保留)
控制是否在 Activity 重新创建期间保留片段实例(例如来自配置更改)。这只能用于不在后堆栈中的片段。如果设置,则重新创建活动时片段生命周期将略有不同:
- onDestroy() 不会被调用(但 onDetach() 仍然会被调用,因为片段正在与其当前活动分离)。
- 由于没有重新创建片段,因此不会调用 onCreate(Bundle)。
- onAttach(活动)和onActivityCreated(捆绑)将仍然被调用。
Question: How do you as a developer use this, and why does it make things easier?
问题:作为开发人员,您如何使用它,为什么它使事情变得更容易?
回答by CommonsWare
How do you as a developer use this
作为开发人员,您如何使用它
Call setRetainInstance(true)
. I typically do that in onCreateView()
or onActivityCreated()
, where I use it.
打电话setRetainInstance(true)
。我通常在onCreateView()
oronActivityCreated()
中使用它。
and why does it make things easier?
为什么它让事情变得更容易?
It tends to be simpler than onRetainNonConfigurationInstance()
for handling the retention of data across configuration changes (e.g., rotating the device from portrait to landscape). Non-retained fragments are destroyed and recreated on the configuration change; retained fragments are not. Hence, any data held by those retained fragments is available to the post-configuration-change activity.
它往往比onRetainNonConfigurationInstance()
处理配置更改中的数据保留更简单(例如,将设备从纵向旋转到横向)。未保留的片段在配置更改时被销毁并重新创建;保留的片段不是。因此,那些保留的片段所持有的任何数据都可用于配置更改后的活动。
回答by DeeV
It's very helpful in keeping long running resources open such as sockets. Have a UI-less fragment that holds references to bluetooth sockets and you won't have to worry about reconnecting them when the user flips the phone.
这对于保持长期运行的资源(例如套接字)打开非常有帮助。拥有一个包含对蓝牙套接字的引用的无 UI 片段,您不必担心在用户翻转手机时重新连接它们。
It's also handy in keeping references to resources that take a long time to load like bitmaps or server data. Load it once, keep it in a retained fragment, and when the activity is reloaded it's still there and you don't have to rebuild it.
保留对需要很长时间加载的资源(如位图或服务器数据)的引用也很方便。加载一次,将它保存在一个保留的片段中,当活动重新加载时,它仍然存在,您不必重建它。
回答by Eurig Jones
Added this answer very late, but I thought it would make things clearer. Say after me. When setRetainInstance
is:
很晚才添加此答案,但我认为它会使事情更清楚。跟我说。什么时候setRetainInstance
:
FALSE
错误的
- Fragment gets re-created on config change. NEW INSTANCE is created.
- ALL lifecycle methods are called on config change, including
onCreate()
andonDestroy()
.
- 片段在配置更改时重新创建。新实例已创建。
- 在配置更改时调用所有生命周期方法,包括
onCreate()
和onDestroy()
。
TRUE
真的
- Fragment does not get re-created on config change. SAME INSTANCE is used.
- All lifecycle methods are called on config change, APART FROM
onCreate()
andonDestroy()
. - Retaining an instance will not work when added to the backstack.
- 片段不会在配置更改时重新创建。使用相同的实例。
- 所有生命周期方法都在配置更改、APART FROM
onCreate()
和onDestroy()
. - 添加到后台堆栈时,保留实例将不起作用。
Don't forget that the above applies to DialogFragment
s as well as Fragments.
不要忘记以上内容适用于DialogFragment
s 以及 Fragments。
回答by Darish
The setRetainInstance(boolean) method is deprecated, use ViewModels instead.
setRetainInstance(boolean) 方法已弃用,请改用 ViewModel。
The setRetainInstance(boolean)
method on Fragments has been deprecatedas of Version 1.3.0of fragment API.
从片段 API 的1.3.0版setRetainInstance(boolean)
开始,不推荐使用Fragments 上的方法。
With the introduction of ViewModels, developers have a specific API for retaining state that can be associated with Activities, Fragments, and Navigation graphs. This allows developers to use a normal, not retained Fragment and keep the specific state they want retained separate.
随着ViewModels的引入,开发人员有一个特定的 API 来保留可以与活动、片段和导航图相关联的状态。这允许开发人员使用正常的、未保留的 Fragment 并将他们想要保留的特定状态分开。
This ensures that developers have a much more understandable lifecycle for those Fragments (one that matches all of the rest of their Fragments) while maintaining the useful properties of a single creation and single destruction (in this case, the constructor of the ViewModel
and the onCleared()
callback from the ViewModel
).
这保证了开发者对于那些片段更加理解生命周期(一个相匹配的所有的片段的其余部分),同时保持一个创造和单破坏的有用性(在这种情况下,的构造ViewModel
和onCleared()
从回调的ViewModel
)。