fragment学习

fragment生命周期

fragment学习_第1张图片


实际问题:

1.如果想在fragment中触发一个事件,用另一个fragment来替换当前fragment。需要在fragment所在的activity的布局文件中用FrameLayout,不能用fragment。

2.全局事务重复提交会报错,应写成局部

3.add和replace的区别

replace是代替某个fragment。

add是在某个fragment上覆盖(如果fragment背景是透明的,会覆盖)

他们只是ui层面的

真正添加到back stack 中的方法是addToBackStack()方法,对add和replace都是有效的。

如果没有addToBackStack ,  则都不会被保存。


A 跳到 B

add             A的fragment会一直运行,不会pause啥的

replace       A的fragment会 pause,stop,destoryView



4.commit

   fragmentTransaction依赖主线程,commit之后需要拿要主线程的执行权限才能执行。

   如果在用户离开寄主activity之后调用commit,会出错。应调用commitAllowingStateLoss()


5.Creating event callbacks to the activity

    activity想要得到fragment返回的事件,可以通过回调的方式:

     在fragment中创建一个内部的接口,让寄主activity实现这个接口。   在attach中判断寄主activity是否实现了该接口。通过接口中方法的回调将信息传递给activity。



6.addBackToStack

    activity被放置在activity的返回栈中由系统进行管理

    fragment只有被自己的fragmentTransaction调用了addBackToStack之后,才会放进返回栈中由宿主activity管理






replace addBackStack


1.one—>two跳转
oneFragment:onPause...
oneFragment:onStop...
oneFragment:onDestroyView...
TwoFragment:onAttach...
TwoFragment:onCreate...
TwoFragment:onCreateView...
TwoFragment:onActivityCreated...
TwoFragment:onStart...
TwoFragment:onResume...

2.two—>one
TwoFragment:onPause...
TwoFragment:onStop...
TwoFragment:onDestroyView...
TwoFragment:onDestroy...
TwoFragment:onDetach...
oneFragment:onCreateView...
oneFragment:onActivityCreated...
oneFragment:onStart...
oneFragment:onResume...


add addBackStack

1.one—>two跳转
TwoFragment:onAttach...
TwoFragment:onCreate...
TwoFragment:onCreateView...
TwoFragment:onActivityCreated...
TwoFragment:onStart...
TwoFragment:onResume...

2.two—>one
TwoFragment:onPause...
TwoFragment:onStop...
TwoFragment:onDestroyView...
TwoFragment:onDestroy...
TwoFragment:onDetach...

replace  


1.one—>two跳转
oneFragment:onPause...
oneFragment:onStop...
oneFragment:onDestroyView...
oneFragment:onDestroy...
oneFragment:onDetach...
TwoFragment:onAttach...
TwoFragment:onCreate...
TwoFragment:onCreateView...
TwoFragment:onActivityCreated...
TwoFragment:onStart...
TwoFragment:onResume...

2.two—>one
TwoFragment:onPause...
TwoFragment:onStop...
TwoFragment:onDestroyView...
TwoFragment:onDestroy...
TwoFragment:onDetach...

add


1.one—>two跳转
TwoFragment:onAttach...
TwoFragment:onCreate...
TwoFragment:onCreateView...
TwoFragment:onActivityCreated...
TwoFragment:onStart...
TwoFragment:onResume...

2.two—>one
oneFragment:onPause...
TwoFragment:onPause...
oneFragment:onStop...
TwoFragment:onStop...
oneFragment:onDestroyView...
oneFragment:onDestroy...
oneFragment:onDetach...
TwoFragment:onDestroyView...
TwoFragment:onDestroy...
TwoFragment:onDetach...















你可能感兴趣的:(fragment学习)