首先明确一点就是Fragment依存于Activity存在的,Fragment的on方法都由所“寄生”的Activity中的on方法调用。
写一个例子来说明启动顺序。
过程
启动:
| 类 |
方法 |
| Activity |
onCreate: |
| Fragment |
onAttach: |
| Fragment |
onCreate: |
| Fragment |
onCreateView: |
| Activity |
onStart: |
| Fragment |
onActivityCreated: |
| Fragment |
onStart: |
| Activity |
onResume: |
| Fragment |
onResume: |
打开别的Activity
| 类 |
方法 |
| Activity |
onPause: |
| Fragment |
onPause: |
| Activity |
onStop: |
| Fragment |
onStop: |
| Activity |
onRestart: |
| Activity |
onStart: |
| Fragment |
onStart: |
| Activity |
onResume: |
| Fragment |
onResume: |
退出
| 类 |
方法 |
| Activity |
onPause: |
| Fragment |
onPause: |
| Activity |
onStop: |
| Fragment |
onStop: |
| Activity |
onDestroy: |
| Fragment |
onDestroyView: |
| Fragment |
onDestroy: |
| Fragment |
onDetach: |
结论
Activity先启动,然后加载Fragment。