26
2017
09

Fragment生命周期

Fragment生命周期

  • 在Viewpager中使用fragment
  • 在FrameLayout中使用Fragment

在Viewpager中使用fragment

  • viewpager中存放3个fragment,viewpager默认加载3个view

生命周期

  • 初次加载viewpager
03-09 11:53:57.212: I/wj(1347): OneFragment:onAttach
03-09 11:53:57.212: I/wj(1347): OneFragment:onCreate
03-09 11:53:57.216: I/wj(1347): OneFragment:onCreateView
03-09 11:53:57.216: I/wj(1347): OneFragment:onViewCreated
03-09 11:53:57.220: I/wj(1347): OneFragment:onActivityCreated
03-09 11:53:57.220: I/wj(1347): OneFragment:onStart
03-09 11:53:57.224: I/wj(1347): OneFragment:onResume
03-09 11:53:57.224: I/wj(1347): TwoFragment:onAttach
03-09 11:53:57.224: I/wj(1347): TwoFragment:onCreate
03-09 11:53:57.224: I/wj(1347): TwoFragment:onCreateView
03-09 11:53:57.224: I/wj(1347): TwoFragment:onViewCreated
03-09 11:53:57.224: I/wj(1347): TwoFragment:onActivityCreated
03-09 11:53:57.224: I/wj(1347): TwoFragment:onStart
03-09 11:53:57.224: I/wj(1347): TwoFragment:onResume
  • 滑动到第二个fragment
03-09 11:55:31.571: I/wj(1347): ThreeFragment:onAttach
03-09 11:55:31.571: I/wj(1347): ThreeFragment:onCreate
03-09 11:55:31.575: I/wj(1347): ThreeFragment:onCreateView
03-09 11:55:31.579: I/wj(1347): ThreeFragment:onViewCreated
03-09 11:55:31.579: I/wj(1347): ThreeFragment:onActivityCreated
03-09 11:55:31.583: I/wj(1347): ThreeFragment:onStart
03-09 11:55:31.591: I/wj(1347): ThreeFragment:onResume
  • 滑动到第三个fragment
03-09 11:56:02.735: I/wj(1347): OneFragment:onPause
03-09 11:56:02.739: I/wj(1347): OneFragment:onStop
03-09 11:56:02.739: I/wj(1347): OneFragment:onDestroyView
  • 滑回第二个fragment
03-09 11:56:31.723: I/wj(1347): OneFragment:onCreateView
03-09 11:56:31.723: I/wj(1347): OneFragment:onViewCreated
03-09 11:56:31.727: I/wj(1347): OneFragment:onActivityCreated
03-09 11:56:31.727: I/wj(1347): OneFragment:onStart
03-09 11:56:31.727: I/wj(1347): OneFragment:onResume
  • 滑回第一个fragment
03-09 11:57:14.087: I/wj(1347): ThreeFragment:onPause
03-09 11:57:14.087: I/wj(1347): ThreeFragment:onStop
03-09 11:57:14.091: I/wj(1347): ThreeFragment:onDestroyView
  • 最后销毁activity
03-09 11:57:46.691: I/wj(1347): OneFragment:onPause
03-09 11:57:46.691: I/wj(1347): TwoFragment:onPause
03-09 11:57:47.223: I/wj(1347): OneFragment:onStop
03-09 11:57:47.223: I/wj(1347): TwoFragment:onStop
03-09 11:57:47.223: I/wj(1347): OneFragment:onDestroyView
03-09 11:57:47.223: I/wj(1347): OneFragment:onDestroy
03-09 11:57:47.223: I/wj(1347): OneFragment:onDetach
03-09 11:57:47.223: I/wj(1347): TwoFragment:onDestroyView
03-09 11:57:47.223: I/wj(1347): TwoFragment:onDestroy
03-09 11:57:47.223: I/wj(1347): TwoFragment:onDetach
03-09 11:57:47.223: I/wj(1347): ThreeFragment:onDestroy
03-09 11:57:47.223: I/wj(1347): ThreeFragment:onDetach

在FrameLayout中使用Fragment

  • 存在两种情况,一个是getFragmentManager().beginTransaction().replace()替换,一个是add(),show(),hide()隐藏显示。两个调用的方法不同。

生命周期

  • 第一次添加fragment
08-23 16:20:28.561: I/memejiang(13348): onAttach...........
08-23 16:20:28.561: I/memejiang(13348): onCreate...........
08-23 16:20:28.562: I/memejiang(13348): onCreateView...........
08-23 16:20:28.564: I/memejiang(13348): onActivityCreated...........
08-23 16:20:28.564: I/memejiang(13348): onStart...........
08-23 16:20:28.564: I/memejiang(13348): onResume...........
  • 调用hide()方法
fragment内部回调该方法
onHiddenChanged(boolean hidden)
  • 调用replace()方法
08-23 16:28:41.373: I/memejiang(17694): onPause...........
08-23 16:28:41.373: I/memejiang(17694): onStop...........
08-23 16:28:41.374: I/memejiang(17694): onDestroyView...........
08-23 16:28:41.374: I/memejiang(17694): onDestroy...........
08-23 16:28:41.374: I/memejiang(17694): onDetach...........
  • 在fragment中启动另一个activity
03-31 16:48:52.604: I/meme(9551): onPause:
  • 返回原activity
03-31 16:48:48.349: I/meme(9551): onResume:
  • activity销毁
08-23 16:22:20.105: I/memejiang(13348): onPause...........
08-23 16:22:20.726: I/memejiang(13348): onStop...........
08-23 16:22:20.731: I/memejiang(13348): onDestroyView...........
08-23 16:22:20.740: I/memejiang(13348): onDestroy...........
08-23 16:22:20.741: I/memejiang(13348): onDetach...........
上一篇:Axure8.0汉化包+注册码 下一篇:拿到view真实宽高的4种方式