fragment各种情况下的生命周期、状态保存startActivityForResult是哪个类的方法,在什么情况下使用?
先上一张fragment的生命周期图:
1、Fragment在Activity中replace
新替换的Fragment:onAttach() > onCreate() > onCreateView ()> onViewCreated() > onActivityCreated ()> onStart ()> onResume()
被替换的Fragment:onPause() > onStop() > onDestroyView ()> onDestroy ()> onDetach()
2、Fragment在Activity中replace,并addToBackStack
新替换的Fragment(没有在BackStack中):onAttach() > onCreate() > onCreateView() > onViewCreated ()> onActivityCreated() > onStart ()> onResume()
新替换的Fragment(已经在BackStack中):onCreateView() > onViewCreated ()> onActivityCreated() > onStart() > onResume()
被替换的Fragment:onPause() > onStop() > onDestroyView()
3、Fragment进入了运行状态:
Fragment在上述的各种情况下进入了onResume()后,则进入了运行状态,以下4个生命周期方法将跟随所属的Activity一起被调用:
onPause() > onStop ()> onStart() > onResume()
关于Fragment的onActivityResult方法:
使用Fragment的startActivity()方法时,FragmentActivity的onActivityResult()方法会回调相应的Fragment的onActivityResult()方法,所以在重写FragmentActivity的onActivityResult()方法时,注意调super.onActivityResult()。