Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發之Activity和Fragment生命周期對比圖

Android開發之Activity和Fragment生命周期對比圖

編輯:關於Android編程

一、Activity 生命周期

\

二、Fragment 生命周期

\

三、對比圖

 


\

四、測試代碼

 

[java]  
  1. package com.goso.testapp;
  2.  
  3. import android.app.Activity;
  4. import android.app.ListFragment;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.ListView;
  12.  
  13. /**
  14. * Demonstration of using ListFragment to show a list of items
  15. * from a canned array.
  16. */
  17. public class FragmentListArray extends Activity {
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. Log.e(HJJ, Activity &&&& onCreate...);
  23. // Create the list fragment and add it as our sole content.
  24. if (getFragmentManager().findFragmentById(android.R.id.content) == null) {
  25. ArrayListFragment list = new ArrayListFragment();
  26. getFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
  27. }
  28. }
  29.  
  30. @Override
  31. protected void onStart() {
  32. // TODO Auto-generated method stub
  33. super.onStart();
  34. Log.e(HJJ, Activity &&&& onStart...);
  35. }
  36.  
  37. @Override
  38. protected void onResume() {
  39. // TODO Auto-generated method stub
  40. super.onResume();
  41. Log.e(HJJ, Activity &&&& onResume...);
  42. }
  43.  
  44. @Override
  45. protected void onStop() {
  46. // TODO Auto-generated method stub
  47. super.onStop();
  48. Log.e(HJJ, Activity &&&& onStop...);
  49. }
  50.  
  51. @Override
  52. protected void onPause() {
  53. // TODO Auto-generated method stub
  54. super.onPause();
  55. Log.e(HJJ, Activity &&&& onPause...);
  56. }
  57.  
  58. @Override
  59. protected void onDestroy() {
  60. // TODO Auto-generated method stub
  61. super.onDestroy();
  62. Log.e(HJJ, Activity &&&& onDestroy...);
  63. }
  64.  
  65. public static class ArrayListFragment extends ListFragment {
  66.  
  67. @Override
  68. public void onAttach(Activity activity) {
  69. // TODO Auto-generated method stub
  70. Log.e(HJJ, ArrayListFragment **** onAttach...);
  71. super.onAttach(activity);
  72. }
  73.  
  74. @Override
  75. public void onCreate(Bundle savedInstanceState) {
  76. // TODO Auto-generated method stub
  77. Log.e(HJJ, ArrayListFragment **** onCreate...);
  78. super.onCreate(savedInstanceState);
  79. }
  80.  
  81. @Override
  82. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  83. Bundle savedInstanceState) {
  84. // TODO Auto-generated method stub
  85. Log.e(HJJ, ArrayListFragment **** onCreateView...);
  86. return super.onCreateView(inflater, container, savedInstanceState);
  87. }
  88.  
  89. @Override
  90. public void onActivityCreated(Bundle savedInstanceState) {
  91. super.onActivityCreated(savedInstanceState);
  92. Log.e(HJJ, ArrayListFragment **** onActivityCreated...);
  93. String[] array = new String[]{C++, JAVA, PYTHON};
  94. setListAdapter(new ArrayAdapter(getActivity(),
  95. android.R.layout.simple_list_item_1, array));
  96. }
  97.  
  98. @Override
  99. public void onStart() {
  100. // TODO Auto-generated method stub
  101. Log.e(HJJ, ArrayListFragment **** onStart...);
  102. super.onStart();
  103. }
  104.  
  105. @Override
  106. public void onResume() {
  107. Log.e(HJJ, ArrayListFragment **** onResume...);
  108. // TODO Auto-generated method stub
  109. super.onResume();
  110. }
  111.  
  112. @Override
  113. public void onPause() {
  114. Log.e(HJJ, ArrayListFragment **** onPause...);
  115. // TODO Auto-generated method stub
  116. super.onPause();
  117. }
  118.  
  119. @Override
  120. public void onStop() {
  121. Log.e(HJJ, ArrayListFragment **** onStop...);
  122. // TODO Auto-generated method stub
  123. super.onStop();
  124. }
  125.  
  126. @Override
  127. public void onDestroyView() {
  128. Log.e(HJJ, ArrayListFragment **** onDestroyView...);
  129. // TODO Auto-generated method stub
  130. super.onDestroyView();
  131. }
  132.  
  133. @Override
  134. public void onDestroy() {
  135. // TODO Auto-generated method stub
  136. Log.e(HJJ, ArrayListFragment **** onDestroy...);
  137. super.onDestroy();
  138. }
  139.  
  140. @Override
  141. public void onDetach() {
  142. Log.e(HJJ, ArrayListFragment **** onDetach...);
  143. // TODO Auto-generated method stub
  144. super.onDetach();
  145. }
  146.  
  147. @Override
  148. public void onListItemClick(ListView l, View v, int position, long id) {
  149. Log.i(FragmentList, Item clicked: + id);
  150. }
  151. }
  152. }
    五、測試結果

     

     

    [java] 
    1. onCreate過程
    2. 01-22 15:30:28.091: E/HJJ(10315): Activity &&&& onCreate...
    3. 01-22 15:30:28.091: E/HJJ(10315): ArrayListFragment **** onAttach...
    4. 01-22 15:30:28.091: E/HJJ(10315): ArrayListFragment **** onCreate...
    5. 01-22 15:30:28.115: E/HJJ(10315): ArrayListFragment **** onCreateView...
    6. 01-22 15:30:28.123: E/HJJ(10315): ArrayListFragment **** onActivityCreated...
    7.  
    8. onStart過程
    9. 01-22 15:30:28.123: E/HJJ(10315): Activity &&&& onStart...
    10. 01-22 15:30:28.123: E/HJJ(10315): ArrayListFragment **** onStart...
    11.  
    12. onResume過程
    13. 01-22 15:30:28.123: E/HJJ(10315): Activity &&&& onResume...
    14. 01-22 15:30:28.123: E/HJJ(10315): ArrayListFragment **** onResume...
    15.  
    16. onPause過程
    17. 01-22 15:31:26.748: E/HJJ(10315): ArrayListFragment **** onPause...
    18. 01-22 15:31:26.748: E/HJJ(10315): Activity &&&& onPause...
    19.  
    20. onStop過程
    21. 01-22 15:31:27.638: E/HJJ(10315): ArrayListFragment **** onStop...
    22. 01-22 15:31:27.638: E/HJJ(10315): Activity &&&& onStop...
    23.  
    24. onStart過程
    25. 01-22 15:31:57.537: E/HJJ(10315): Activity &&&& onStart...
    26. 01-22 15:31:57.537: E/HJJ(10315): ArrayListFragment **** onStart...
    27.  
    28. onResume過程
    29. 01-22 15:31:57.537: E/HJJ(10315): Activity &&&& onResume...
    30. 01-22 15:31:57.537: E/HJJ(10315): ArrayListFragment **** onResume...
    31.  
    32. onPause過程
    33. 01-22 15:32:47.412: E/HJJ(10315): ArrayListFragment **** onPause...
    34. 01-22 15:32:47.412: E/HJJ(10315): Activity &&&& onPause...
    35.  
    36. onStop過程
    37. 01-22 15:32:47.865: E/HJJ(10315): ArrayListFragment **** onStop...
    38. 01-22 15:32:47.865: E/HJJ(10315): Activity &&&& onStop...
    39.  
    40. onDestroy過程
    41. 01-22 15:32:47.865: E/HJJ(10315): ArrayListFragment **** onDestroyView...
    42. 01-22 15:32:47.865: E/HJJ(10315): ArrayListFragment **** onDestroy...
    43. 01-22 15:32:47.865: E/HJJ(10315): ArrayListFragment **** onDetach...
    44. 01-22 15:32:47.865: E/HJJ(10315): Activity &&&& onDestroy...
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved