Fragment与Activity交互

来源:互联网 发布:three.js视频教程下载 编辑:程序博客网 时间:2024/04/29 06:19

Activity直接影响它所包含的Fragment的生命周期,所以对Activity的某个生命周期方法的调用也会产生对Fragment相同方法的调用。例如:当activity的onPause()方法被调用时,它所包含的所有的fragment们的onPause()方法都会被调用。

Fragment比Activity还要多出几个生命周期回调方法,这些额外的方法是为了与activity的交互而设立的。

onAttach():当fragment和activity被关联时调用。

onCreateView():当创建fragment的UI被初始化时调用。

onActivityCreated():当activity的onCreate()方法返回时调用。

onDestroyView():当fragment的UI被移除的时候调用。

onDetach():当fragment和activity去关联时调用。

Android官网解释如下:

onAttach():Called when the fragment has beenassociated with the activity (the Activity is passed in here).

onCreateView():Called to create the view hierarchyassociated with the fragment.

onActivityCreated():Called when the activity'sonCreate() method has returned.

onDestroyView():Called when the view hierarchyassociated with the fragment is being removed.

onDetach():Called when the fragment is beingdisassociated from the activity.



0 0
原创粉丝点击