1.开启一个新的 AFragment
onAttach()→onCreate()→onCreateView()→onViewCreated()→onStart()→onResume()
2.从 AFragment 跳转到 Activity1
onPause()→onStop()
3.从 Activity1 跳回到 AFragment
onStart()→onResume()
4.从 AFragment 跳转到新的 BFragment
先运行BFragment生命周期 onAttach()→onCreate()→onCreateView()→onViewCreated()→onStart()→onResume()
再运行AFragment生命周期 onPause()→onStop()→onDestoryView()
5.从 BFragment 跳回到 AFragment
先运行AFragment生命周期 onCreateView()→onViewCreated()→onStart()→onResume()
再运行BFragment生命周期 onPause()→onStop()→onDestoryView()
6.从 AFragment 跳回到 BFragment
先运行BFragment生命周期onCreateView()→onViewCreated()→onStart()→onResume()
再运行AFragment生命周期onPause()→onStop()→onDestoryView()
总结:
从上述生命周期可以观察出
1.Fragment只会onAttach()→onCreate()一次。
2.Fragment之间切换每次先走新生Fragment生命周期,旧Fragment会被onPause()→onStop()→onDestoryView()。
3.Fragment与Activity之间切换,Fragment 走只走onPause()→onStop(),重新回来走onStart()→onResume()。