Activity与Fragment之间生命周期比较

Fragment 是 Activity 的一个组件片段,也就是说他的生命周期

是依赖于 Activity 的

但是它比 Activity 多了几个生命步骤,

  1. 首先是Activity#onCreate 中Fragment会执行onAttach()、onCreate()、onCreateView()、onActivityCreated()
  2. 接着Activity#onStart()中Fragment也会执行onStart()
  3. 然后是Activity#onResume()中Fragment也会执行onResume()
  4. 接着是Activity#onPause()中Fragment也会执行onPause()(先于Activity执行)
  5. 然后是Activity#onStop()中Fragment也会执行onStop()(先于Activity执行)
  6. 接着是Activity#onDestroy()中Fragment会执行onDestroyView()、onDestroy()、onDetach()(先于Activity执行)

猜你喜欢

转载自blog.csdn.net/github_37130188/article/details/89199363