android 温故知新之fragment中易忽视的onViewStateRestored()

本文详细介绍了Android中Fragment的生命周期,包括onAttach、onCreate、onCreateView等关键方法的作用及调用时机,并补充了onViewStateRestored方法的相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

回顾知识,发现问题!

看了很多网上关于fragment的讲解教程,发现很多都少了onViewStateRestored()这个周期,在此补充。

参考:http://www.android-doc.com/reference/android/app/Fragment.html#Lifecycle,做如下翻译,不足忘回馈。

onAttach(Activity) :
    called once the fragment is associated with its activity.
    调用该方法,将fragment与activity相关联

onCreate(Bundle) :
    called to do initial creation of the fragment.
    调用该方法,将fragment初始化;

onCreateView(LayoutInflater, ViewGroup, Bundle) :
    creates and returns the view hierarchy associated with the fragment.
    调用该方法,创建视图,返回结果View;

onActivityCreated(Bundle):
    tells the fragment that its activity has completed its own Activity.onCreate().
    调用该方法,通知fragment,activity中的oncreate()方法已完成;

onViewStateRestored(Bundle):
    tells the fragment that all of the saved state of its view hierarchy has been restored.  
    调用该方法,通知fragment,该视图层已保存;

onStart():
    makes the fragment visible to the user (based on its containing activity being started).
    调用该方法,和activity的started方法相关联,将fragment展示给用户;

onResume():
    makes the fragment interacting with the user (based on its containing activity being resumed). 
    调用该方法,和activity的resumed方法相关联,使fragment与用户交互;

As a fragment is no longer being used, it goes through a reverse series of callbacks:
调用以下的方法,fragment将不再被使用;

onPause():
    fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
    调用该方法,fragment不与用户进行交互,此时activity的paused方法被调用,或者fragment正在被activity修改;

onStop():
    fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
    调用该方法,fragment不再展示,此时activity的stopped方法被调用,或者fragment正在被activity修改;

onDestroyView():
    allows the fragment to clean up resources associated with its View.
    调用该方法,fragment清除掉资源与该activity的关联;

onDestroy() :
    called to do final cleanup of the fragment's state.
    调用该方法,清除fragment的状态;

onDetach():
    called immediately prior to the fragment no longer being associated with its activity. 
    调用该方法,清除fragment与该activity的关联;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值