Fragment设置对象不销毁!
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.
在onCreate函数设置了这个函数后,就可以使得fragment对象里面的成员数据不被置空。这样就使得我们不必在旋转屏幕的时候要将数据进行持久化,之后再读处理。
下面有一篇文章对Activity的lifecycle的解析非常不错。
本文介绍如何使用setRetainInstance方法使Fragment在Activity重建时不销毁,避免了因屏幕旋转导致的数据丢失问题。通过此方法,可以简化应用开发流程,提高开发效率。
4171

被折叠的 条评论
为什么被折叠?



