码迷,mamicode.com
首页 > 移动开发 > 详细

Android之Activity,Fragment生命周期探知

时间:2015-03-17 20:00:49      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:

Android之Activity,Fragment生命周期探知 - sin@ - 博客频道 - CSDN.NET

根据官方文档,本人手画了一下Activity和Fragment的整个生命周期执行顺序图:

Activity生命周期执行顺序图

技术分享

Fragment生命周期执行顺序图

技术分享

?
一个Activity在它的整个生命周期里面分三个状态,分别是活动周期,可见周期,以及生命周期。 一个Fragment是依附于Activity之上,它的整个生命周期里面也分三个状态,分别是活动周期,可见周期,以及生命周期。 现在我们来分析一下这些状态的实际执行顺序,我们通过日志的方式来打印每个函数的执行过程,新建一个android工程,Activity代码和Fragment代码如下:
  1. public?class?MainActivity?extends?Activity?{??
  2. ??
  3. ????private?static?String?TAG?=?"chenjianjun";??
  4. ??????
  5. ????private?static?Button?mybtnButton;??
  6. ??????
  7. ????@Override??
  8. ????public?boolean?onCreateOptionsMenu(Menu?menu)?{??
  9. ????????//?Inflate?the?menu;?this?adds?items?to?the?action?bar?if?it?is?present.??
  10. ????????getMenuInflater().inflate(R.menu.main,?menu);??
  11. ????????return?true;??
  12. ????}??
  13. ??????
  14. ????/**??
  15. ?????*?在完整生存期开始时调用?
  16. ?????*?@see?android.app.Activity#onCreate(android.os.Bundle)?
  17. ?????*/??
  18. ????@Override??
  19. ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  20. ????????super.onCreate(savedInstanceState);??
  21. ????????setContentView(R.layout.activity_main);??
  22. ??????????
  23. ????????mybtnButton?=?(Button)findViewById(R.id.btn);??
  24. ????????mybtnButton.setOnClickListener(new?View.OnClickListener()?{??
  25. ??????????????
  26. ????????????@Override??
  27. ????????????public?void?onClick(View?v)?{??
  28. ????????????????Intent?bintent?=?new?Intent(MainActivity.this,?SecondActivity.class);??
  29. ????????????????startActivity(bintent);??
  30. ????????????}??
  31. ????????});??
  32. ??????????
  33. ????????Log.i(TAG,?"onCreate?is?call.......");??
  34. ????}??
  35. ??
  36. ????/**?
  37. ?????*?在onCreate方法后调用,用于恢复UI状态?
  38. ?????*?@see?android.app.Activity#onRestoreInstanceState(android.os.Bundle)?
  39. ?????*/??
  40. ????@Override??
  41. ????protected?void?onRestoreInstanceState(Bundle?savedInstanceState)??
  42. ????{??
  43. ????????Log.i(TAG,?"onRestoreInstanceState?is?call?start.......");??
  44. ????????super.onRestoreInstanceState(savedInstanceState);??
  45. ????????/**?
  46. ?????????*?从savedInstanceState恢复UI状态?
  47. ?????????*?这个savedInstanceState也被传递给了onCreate?
  48. ?????????*?自Activity上次可见之后,只有当系统终止了该Activity时,才会被调用?
  49. ?????????*?在随后的Activity进程的可见生存期之前被调用?
  50. ?????????*/??
  51. ????????Log.i(TAG,?"onRestoreInstanceState?is?call?end.......");??
  52. ????}??
  53. ??????
  54. ????/**?
  55. ?????*?在可见生存期开始时调用?
  56. ?????*?@see?android.app.Activity#onStart()?
  57. ?????*/??
  58. ????@Override??
  59. ????protected?void?onStart()??
  60. ????{??
  61. ????????Log.i(TAG,?"onStart?is?call?start.......");??
  62. ????????super.onStart();??
  63. ????????/**?
  64. ?????????*?既然Activity可见,就应用任何要求的UI变化?
  65. ?????????*/??
  66. ????????Log.i(TAG,?"onStart?is?call?end.......");??
  67. ????}??
  68. ??????
  69. ????/**?
  70. ?????*?在Activity活动状态生存期开始时调用?
  71. ?????*?@see?android.app.Activity#onResume()?
  72. ?????*/??
  73. ????@Override??
  74. ????protected?void?onResume()??
  75. ????{??
  76. ????????Log.i(TAG,?"onResume?is?call?start.......");??
  77. ????????super.onResume();??
  78. ????????/**?
  79. ?????????*?恢复Activity处于不活动状态时被挂起的,暂停的UI更新,线程,进程?
  80. ?????????*/??
  81. ????????Log.i(TAG,?"onResume?is?call?end.......");??
  82. ????}??
  83. ??????
  84. ????/*?
  85. ?????*?把UI状态保存到outState中?
  86. ?????*?@see?android.app.Activity#onSaveInstanceState(android.os.Bundle)?
  87. ?????*/??
  88. ????@Override??
  89. ????protected?void?onSaveInstanceState(Bundle?savedInstanceState)??
  90. ????{??
  91. ????????Log.i(TAG,?"onSaveInstanceState?is?call?start.......");??
  92. ????????/**?
  93. ?????????*?保存UI的状态?
  94. ?????????*?如果进程被运行时终止并重启,那么这个Bundle会被传递给onCreate和onRestoreInstanceState?
  95. ?????????*/??
  96. ????????super.onSaveInstanceState(savedInstanceState);??
  97. ????????Log.i(TAG,?"onSaveInstanceState?is?call?end.......");??
  98. ????}??
  99. ??????
  100. ????/**?
  101. ?????*?在Activity活动状态生存期结束时被调用?
  102. ?????*/??
  103. ????@Override??
  104. ????protected?void?onPause()??
  105. ????{??
  106. ????????Log.i(TAG,?"onPause?is?call?start.......");??
  107. ????????/**?
  108. ?????????*?挂起UI更新,线程或者CPU密集的进程?
  109. ?????????*/??
  110. ????????super.onPause();??
  111. ????????Log.i(TAG,?"onPause?is?call?end.......");??
  112. ????}??
  113. ??????
  114. ????@Override??
  115. ????protected?void?onStop()??
  116. ????{??
  117. ????????Log.i(TAG,?"onStop?is?call?start.......");??
  118. ????????/**?
  119. ?????????*?挂起UI更新,线程或者处理?
  120. ?????????*?当Activity不可见时,保存所有的编辑或者状态改变?
  121. ?????????*?调用这个方法后,进程可能会被终止?
  122. ?????????*/??
  123. ????????super.onStop();??
  124. ??????????
  125. ????????Log.i(TAG,?"onStop?is?callv?end.......");??
  126. ????}??
  127. ??????
  128. ????@Override??
  129. ????protected?void?onDestroy()??
  130. ????{??
  131. ????????Log.i(TAG,?"onDestroy?is?call?start.......");??
  132. ????????/**?
  133. ?????????*?清理所有的资源,包括结束线程,关闭数据库连接等?
  134. ?????????*/??
  135. ????????super.onDestroy();??
  136. ??????????
  137. ????????Log.i(TAG,?"onDestroy?is?call?end.......");??
  138. ????}??
  139. ??????
  140. ????@Override??
  141. ????protected?void?onRestart()??
  142. ????{??
  143. ????????Log.i(TAG,?"onRestart?is?call?start.......");??
  144. ????????super.onRestart();??
  145. ????????/**?
  146. ?????????*?加装载改变,知道Activity在此进程中已经可见?
  147. ?????????*/??
  148. ????????Log.i(TAG,?"onRestart?is?call?end.......");??
  149. ????}??
  150. }??

?

  1. public?class?MyStateFragment?extends?Fragment?{??
  2. ??
  3. ????private?static?String?TAG?=?"chenjianjun";??
  4. ????/**?
  5. ?????*?调用该方法时,Fragment会被连接到它的父Activity上?
  6. ?????*/??
  7. ????@Override??
  8. ????public?void?onAttach(Activity?activity)??
  9. ????{??
  10. ????????Log.i(TAG,?"-----onAttach?is?call?start.......");??
  11. ????????super.onAttach(activity);??
  12. ????????/**?
  13. ?????????*?获取对父Activity的引用?
  14. ?????????*/??
  15. ????????Log.i(TAG,?"-----onAttach?is?call?end.......");??
  16. ????}??
  17. ??????
  18. ????/**?
  19. ?????*?调用该方法来进行Fragment的初始创建?
  20. ?????*/??
  21. ????@Override??
  22. ????public?void?onCreate(Bundle?savedInstanceState)??
  23. ????{??
  24. ????????Log.i(TAG,?"-----onCreate?is?call?start.......");??
  25. ????????super.onCreate(savedInstanceState);??
  26. ????????/**?
  27. ?????????*?初期化Fragment?
  28. ?????????*/??
  29. ????????Log.i(TAG,?"-----onCreate?is?call?end.......");??
  30. ????}??
  31. ??????
  32. ????/**?
  33. ?????*?一旦Fragment已被创建,要创建它自己的用户界面时调用该方法?
  34. ?????*/??
  35. ????@Override??
  36. ????public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,?Bundle?savedInstanceState)??
  37. ????{??
  38. ????????Log.i(TAG,?"-----onCreateView?is?call?start.......");??
  39. ????????/**?
  40. ?????????*?创建,或者填充Fragment的UI,并返回它?
  41. ?????????*?如果这个Fragment没有UI,那么返回null?
  42. ?????????*/??
  43. ????????View?view?=?inflater.inflate(R.layout.my_state_fragment,?container,?false);??
  44. ??????????
  45. ????????Log.i(TAG,?"-----onCreateView?is?call?end.......");??
  46. ????????return?view;??
  47. ????}??
  48. ??????
  49. ????/**?
  50. ?????*?一旦父Activity和Fragment的UI已经被创建,则调用该方法?
  51. ?????*/??
  52. ????@Override??
  53. ????public?void?onActivityCreated(Bundle?savedInstanceState)??
  54. ????{??
  55. ????????Log.i(TAG,?"-----onActivityCreated?is?call?start.......");??
  56. ????????super.onActivityCreated(savedInstanceState);??
  57. ????????/**?
  58. ?????????*?完成Fragment的初期化----那些父Activity和Fragment的View被完全填充后才能做的事情?
  59. ?????????*/??
  60. ????????Log.i(TAG,?"-----onActivityCreated?is?call?end.......");??
  61. ????}??
  62. ??????
  63. ????/**?
  64. ?????*?在可见生命周期的开始时被调用?
  65. ?????*/??
  66. ????@Override??
  67. ????public?void?onStart()??
  68. ????{??
  69. ????????Log.i(TAG,?"-----onStart?is?call?start.......");??
  70. ????????super.onStart();??
  71. ????????/**?
  72. ?????????*?应用所有需要的UI变化,现在Fragment是可见的?
  73. ?????????*/??
  74. ????????Log.i(TAG,?"-----onStart?is?call?end.......");??
  75. ????}??
  76. ??????
  77. ????/**?
  78. ?????*?在活动生命周期开始时被调用?
  79. ?????*/??
  80. ????@Override??
  81. ????public?void?onResume()??
  82. ????{??
  83. ????????Log.i(TAG,?"-----onResume?is?call?start.......");??
  84. ????????super.onResume();??
  85. ????????/**?
  86. ?????????*?恢复所有暂停的事物,如UI更新,线程或者进程等?
  87. ?????????*/??
  88. ????????Log.i(TAG,?"-----onResume?is?call?end.......");??
  89. ????}??
  90. ??????
  91. ????/**?
  92. ?????*?在活动生命周期结束时被调用?
  93. ?????*/??
  94. ????@Override??
  95. ????public?void?onPause()??
  96. ????{??
  97. ????????Log.i(TAG,?"-----onPause?is?call?start.......");??
  98. ????????/**?
  99. ?????????*?需要暂停父Activity不是活动的前台Activity时,需要暂停的事物,如UI更新,线程或者进程等?
  100. ?????????*/??
  101. ????????super.onPause();??
  102. ????????Log.i(TAG,?"-----onPause?is?call?end.......");??
  103. ????}??
  104. ??????
  105. ????/**?
  106. ?????*??在活动生命周期结束时被调用,保存UI的状态变化?
  107. ?????*/??
  108. ????@Override??
  109. ????public?void?onSaveInstanceState(Bundle?savedInstanceState)??
  110. ????{??
  111. ????????Log.i(TAG,?"-----onSaveInstanceState?is?call?start.......");??
  112. ????????/**?
  113. ?????????*?将UI的变化信息保存到savedInstanceState中?
  114. ?????????*?这个savedInstanceState会被传递到onCreate,onCreateView和onActivityCreated(如果它的父Activity被终止并且重新启动)方法中?
  115. ?????????*/??
  116. ????????super.onSaveInstanceState(savedInstanceState);??
  117. ????????Log.i(TAG,?"-----onSaveInstanceState?is?call?end.......");??
  118. ????}??
  119. ??????
  120. ????/**?
  121. ?????*?在可见生命周期结束时调用该方法?
  122. ?????*/??
  123. ????@Override??
  124. ????public?void?onStop()??
  125. ????{??
  126. ????????Log.i(TAG,?"-----onStop?is?call?start.......");??
  127. ????????/**?
  128. ?????????*?当Fragment不可见时,暂停其余的事物,如UI更新,线程或者进程等?
  129. ?????????*/??
  130. ????????super.onStop();??
  131. ????????Log.i(TAG,?"-----onStop?is?call?end.......");??
  132. ????}??
  133. ??????
  134. ????/**?
  135. ?????*?当Fragment的view被分离时,调用该方法?
  136. ?????*/??
  137. ????@Override??
  138. ????public?void?onDestroyView()??
  139. ????{??
  140. ????????Log.i(TAG,?"-----onDestroyView?is?call?start.......");??
  141. ????????/**?
  142. ?????????*?清除资源相关的view?
  143. ?????????*/??
  144. ????????super.onDestroyView();??
  145. ????????Log.i(TAG,?"-----onDestroyView?is?call?end.......");??
  146. ????}??
  147. ??????
  148. ????/**?
  149. ?????*?在整个生命周期结束的时候调用?
  150. ?????*/??
  151. ????@Override??
  152. ????public?void?onDestroy()??
  153. ????{??
  154. ????????Log.i(TAG,?"-----onDestroy?is?call?start.......");??
  155. ????????/**?
  156. ?????????*?清除所有的资源,包括线程和关闭数据库连接等?
  157. ?????????*/??
  158. ????????super.onDestroy();??
  159. ????????Log.i(TAG,?"-----onDestroy?is?call?end.......");??
  160. ????}??
  161. ??????
  162. ????/**?
  163. ?????*?当Fragment从它的父Activity上分离时调用?
  164. ?????*/??
  165. ????@Override??
  166. ????public?void?onDetach()??
  167. ????{??
  168. ????????Log.i(TAG,?"-----onDetach?is?call?start.......");??
  169. ????????super.onDetach();??
  170. ????????Log.i(TAG,?"-----onDetach?is?call?end.......");??
  171. ????}??
  172. }??

activity_main.xml:

  1. <RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  2. ????xmlns:tools="http://schemas.android.com/tools"??
  3. ????android:layout_width="match_parent"??
  4. ????android:layout_height="match_parent"??
  5. ????android:paddingBottom="@dimen/activity_vertical_margin"??
  6. ????android:paddingLeft="@dimen/activity_horizontal_margin"??
  7. ????android:paddingRight="@dimen/activity_horizontal_margin"??
  8. ????android:paddingTop="@dimen/activity_vertical_margin"??
  9. ????tools:context=".MainActivity"?>??
  10. ??
  11. ????<fragment???
  12. ????????android:name="com.xuexi.mystateactivity.fragment.MyStateFragment"??
  13. ????????android:id="@+id/ToDoListFragment"??
  14. ????????android:layout_width="match_parent"??
  15. ????????android:layout_height="wrap_content"??
  16. ????????android:visibility="gone"???
  17. ????????/>??
  18. ??????
  19. ????<Button??
  20. ????????android:id="@+id/btn"??
  21. ????????android:layout_width="wrap_content"??
  22. ????????android:layout_height="wrap_content"??
  23. ????????android:layout_alignLeft="@+id/ToDoListFragment"??
  24. ????????android:layout_below="@+id/ToDoListFragment"??
  25. ????????android:layout_marginTop="44dp"??
  26. ????????android:text="@string/btn_name"?/>??
  27. ??
  28. </RelativeLayout>??



编译运行,启动程序打印日志如下:

技术分享

切换到后台,日志如下:

技术分享

从后台切换到前台,日志如下:

技术分享

切换到其他的activity,日志如下:

技术分享

从其他的activity切换回来,日志如下:

技术分享

按home键退出,日志如下:

技术分享

综上,大家对照代码查看日志的,我们展现了在每种情况下,Activity和Fragment的周期处于那种状态。理解好Activity和Fragment的生命周期,对于我们开发好一个app程序

有很大的帮助。

这里有个问题,跟文档上面有一点不相符的,就是Activity的onSaveInstanceState调用,在切换到后台和切换到其他Activity的时候有调用,但是都是在onPause和onStop之间,

书中的文字表述也是这样来说的,不知道为什么图是这样来画的!

Android之Activity,Fragment生命周期探知

标签:

原文地址:http://www.cnblogs.com/seven1979/p/4345121.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!