Fragment与Activity交互

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.


Fragment与Activity交互_第1张图片

你可能感兴趣的:(Activity,Fragment)