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.
Also accessing the view hierarchy of the parent activity must be done in the onActivityCreated, not sooner.
这是stackoverflow的一个问答的答案,原文的链接http://stackoverflow.com/questions/8041206/android-fragment-oncreateview-vs-onactivitycreated
翻译成中文是:
如果你的view是静态的,没必要把任何方法移动到onActivityCreate,但是当你--举个例子,从适配器填充list的时候,你应该在onActivityCreated 里面做,并在用setRetainInstance做的时候保存view的状态。
而且,在访问父activity的view层的时候必须在该方法里面做,而不是后面的方法。
简单来讲说了3个:
1、静态的view不需要onActivityCreated
2、保存view的状态的时候需要用onActivityCreated
3、访问父activity的view层的时候需要在onActivityCreated 方法里面做
什么是staticview呢,这篇文章里面给了解释:
he meant by static view that the view which is displayed to the user is nothing but the inflated xml layout. No modification in coding or at runtime
是说展示给用户的时候只是填充了布局,没有用代码或者在运行时修改。(我的理解是没有在代码中修改view也没有在运行时由于某种条件改变该view)
大家如果看到原网址的话会发现里面有句很有哲理的话,对一个问题进行回答时说的
question:
|
Why its not required in case of 'static'? It seems everyone got this but i didnt.
为什么static里面不需要onActivityCreate呢,好像每个人都知道就我不知道。(我们也经常有这种想法)
| |
answer:
|
Don't think everyone got it, Diffy, but they were just happy with the answer
不要认为每个人都懂,Diffy,他们仅仅是对于答案感觉满意罢了。(这就是哲理,好多时候我们不是懂了不是会了,而是对答案满意,觉得答案没有错,然后我们其实并不懂)
|