fragment的onActivityCreate应该干什么

原文:


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,他们仅仅是对于答案感觉满意罢了。(这就是哲理,好多时候我们不是懂了不是会了,而是对答案满意,觉得答案没有错,然后我们其实并不懂)



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值