采用片段的setRetainInstance(真)真的是一个很好的做法来处理旋转变化 [英] Is using Fragment's setRetainInstance(true) really a good practice to handle rotation change

查看:156
本文介绍了采用片段的setRetainInstance(真)真的是一个很好的做法来处理旋转变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是为什么使用片段#setRetainInstance(布尔)?

我想问这样的原因是活动来处理旋转,的官方活动文档鼓励我们,让活动关机和旋转过程中重新启动。

  

安卓configChanges列出配置更改活动   将处理本身。当配置更改发生在运行时,   活动关闭,默认情况下重新启动,但声明   与此属性配置将$ P $被pvent活动   重新启动。相反,该​​活动仍在运行和   onConfigurationChanged()方法被调用。注意:使用这个属性   应避免使用只能作为最后手段。请阅读处理   关于如何妥善处理的详细信息运行时更改   重新启动由于配置更改。

任何试图改变这一活动默认行为似乎是不好的做法。为了避免活动从换料时间重新启动过程中消耗的数据结构,我们做使用 onRetainNonConfigurationInstance getLastNonConfigurationInstance 的。 - 官方处理运行时更改

然而,当涉及到处理旋转的片段,确实谷歌给我们带来不同的建议?他们不希望我们关闭并重新启动片段?

<一个href="http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance%28%29">public对象onRetainNonConfigurationInstance()

  

此方法去precated在API层面13.使用新的片段API   setRetainInstance(布尔值)代替;这也适用于旧的   通过Android兼容包的平台。

  1. 为什么谷歌鼓励我们关停和旋转过程中重新启动活动,但鼓励我们要保留片段旋转过​​程中?
  2. 如果 setRetainInstance(真)在办理转好,为什么不谷歌使其作为片段的默认行为?
解决方案
  • 的配置修改:时,突然屏幕变得更广泛,高度(典型景观)要少得多,容易为可视组件更新其显示和更智能地使用可用的屏幕。配置变化的另一个例子是用户滑动所述硬件键盘,设备语言改变,等等。为什么要重新启动:

    • Android组件的青睐声明布局,加载了一堆XML布局,并从那里工作。寻找每一个查看和重新排列/实时更新将是一个烂摊子,更何况所有的事件处理程序和其他自定义视图code的重新布线。它的方式更容易地重新加载另外一堆布局文件。

    • 按需
    • 此外,在Android中,活动形式的住在制度的摆布,所以很自然,活动的生命周期是这样设计(推荐),它能够重新创建自己,任何时候,只要因为它是之前被摧毁。这种模式可容纳全部重新启动,这是由于配置更改为好。如果你把你的活动,并能够保持一个永恒的状态的片段,配置更改将不会是太大的问题。

    • 保留状态数据(模型),而不是显示它的东西(UI和视图)。

  • setRetainInstance(真):建议只用碎片使用不抱任何提及什么,将要重新创建在旋转。这意味着你不应该在保存上下文,视图等典型的视觉片段做任何片段使用它。但它与持有对象,如正在运行的线程,AsyncTasks,数据采集,加载资源,获取结果等。这种方法有助于在使用非视觉的片段,作为一个可拆卸的支架,一个活动的非上下文相关对象片段非常有用

I'm referring to Why use Fragment#setRetainInstance(boolean)?

The reason I ask so is for Activity to handle rotation, Official Activity Documentation encourages us to let Activity shut-down and restart during rotation.

android:configChanges Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called. Note: Using this attribute should be avoided and used only as a last-resort. Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change.

Any attempt to change this Activity default behavior seems to be bad practice. To avoid Activity from reloading time consuming data structure during restarting, we make make use of onRetainNonConfigurationInstance and getLastNonConfigurationInstance. - Official Handling Runtime Changes

However, when comes to handling rotation in Fragment, does Google give us different recommendation? They do not want us to shut down and restart Fragment?

public Object onRetainNonConfigurationInstance ()

This method was deprecated in API level 13. Use the new Fragment API setRetainInstance(boolean) instead; this is also available on older platforms through the Android compatibility package.

  1. Why does Google encourage us to shut down and restart Activity during rotation, but encourage us to retain Fragment during rotation?
  2. If setRetainInstance(true) is good in handling rotation, why don't Google make it as Fragment's default behavior?

解决方案

  • Configuration changes: when suddenly screen becomes much wider and much less in height (typical landscape), it is apt for a visual component to update its display and more intelligently use the screen available. Another examples of config change are user sliding the hardware keyboard, device language changing, and so on. why re-start :

    • Android components favor declarative layout, you load a bunch of XML layouts, and work from there. Finding every View and re-arranging/updating it in real time will be a mess, not to mention the re-wiring of all the event handlers and other custom View code. Its way easier to reload another bunch of layout files.

    • Also, In Android, Activities kind of live at the mercy of system, so naturally, Activity life cycle is so designed (and recommended) that it is capable of re-creating itself on demand , any time, just as it was before it was destroyed. This pattern accommodates all re-starts, those due to configuration changes as well. If you make your Activities and Fragments capable of maintaining an eternal state, configuration changes won't be that much of a problem.

    • Retain state data (Models), not the stuff displaying it (UI and Views).

  • setRetainInstance(true): It is recommended only to be used with fragments that do not hold any reference to anything, that will be recreated on rotation. This means you should not use it on any Fragment that holds Context, Views, etc. A typical Visual fragment does. But it is very useful with Fragments that hold objects like running Threads, AsyncTasks, Data Collections, loaded assets, fetched results etc. This method helps in using a non visual Fragment, as a detachable holder, for non Context-dependent objects of an Activity.

这篇关于采用片段的setRetainInstance(真)真的是一个很好的做法来处理旋转变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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