本文介绍了Android的碎片onCreateView与onActivityCreated的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,一个片段的视图层次结构在onCreateView被夸大,但可以在onCreateView还有什么其他的功能对哪些应该等待onActivityCreated?我目前的实现使用单独活动的一切,和一个典型的活动做了大部分工作在其onCreate方法,包括膨胀的观点,设置内容来看,初始化各种小部件数据,建立监听器等。

I know that a fragment's view hierarchy has to be inflated in onCreateView, but what other functionality can be in onCreateView vs. what should wait for onActivityCreated? My current implementation uses separate activities for everything, and a typical activity does most of its work in its onCreate method, including inflating the view, setting the content view, initializing the various widgets with data, setting up listeners, etc.

所以,可以这样大概都被转移到onCreateView,还是应该有些功能被放入onActivityCreated方法呢?

So can this probably all be moved into onCreateView, or should some functions be put into an onActivityCreated method instead?

推荐答案

如果你的观点是静态的,那么动任何code到onActivityCreated方法是没有必要的。但是当你 - 例如,填补适配器一些名单,那么你应该做的onActivityCreated方法以及恢复视图状态时setRetainInstance来这样做。

If your view is static, then moving any code to the onActivityCreated method is not necessary. But when you - for instance, fill some lists from the adapter, then you should do it in the onActivityCreated method as well as restoring the view state when setRetainInstance used to do so.

另外访问父活动的层次来看必须在onActivityCreated做的,不是越快。

Also accessing the view hierarchy of the parent activity must be done in the onActivityCreated, not sooner.

这篇关于Android的碎片onCreateView与onActivityCreated的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-14 19:55