Activity的生命周期

来源:互联网 发布:usb转ttl怎么接单片机 编辑:程序博客网 时间:2024/05/29 12:33

Activity这个类,定义了一些回调函数来控制它的生命周期。

  • onCreate()  ——  当Activity第一次创建的时候被调用。
  • onStart()  ——  当Activity对用户可见的时候被调用。
  • onResume()  ——  当Activity开始和用户交互的时候被调用。
  • onPause()  ——  正在运行的Activity马上要被暂停的时候被调用,此时,在这之前的Activity被重新获取。
  • onStop()  ——  当Activity不在对用户可见的时候被调用。

默认地,被创建的Activity中都包含一个onCreate()方法,通过这个方法,可以创建显示给用户的UI组件。

从“被创建”到“被销毁”的生命周期图示:

想要理解Activity生命周期的最好办法就是创建一个工程,并实现所有的回调函数,然后让Activity与用户交互。

1、创建一个工程:Activity101。

2、Activity101Activity.java中的代码。

[java] view plaincopy
  1. public class Activity101Activity extends Activity {  
  2.     String tag = "Lifecycle";  
  3.   
  4.     /** Called when the activity is first created. */  
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.   
  9.         setContentView(R.layout.main);  
  10.         Log.d(tag, "In the onCreate() event");  
  11.     }  
  12.   
  13.     public void onStart() {  
  14.         super.onStart();  
  15.         Log.d(tag, "In the onStart() event");  
  16.     }  
  17.   
  18.     public void onRestart() {  
  19.         super.onRestart();  
  20.         Log.d(tag, "In the onRestart() event");  
  21.     }  
  22.   
  23.     public void onResume() {  
  24.         super.onResume();  
  25.         Log.d(tag, "In the onResume() event");  
  26.     }  
  27.   
  28.     public void onPause() {  
  29.         super.onPause();  
  30.         Log.d(tag, "In the onPause() event");  
  31.     }  
  32.   
  33.     public void onStop() {  
  34.         super.onStop();  
  35.         Log.d(tag, "In the onStop() event");  
  36.     }  
  37.   
  38.     public void onDestroy() {  
  39.         super.onDestroy();  
  40.         Log.d(tag, "In the onDestroy() event");  
  41.     }  
  42. }  

3、按F11在模拟器上调试。

4、当这个activity第一次被加载:

[plain] view plaincopy
  1. 03-23 01:54:32.602: D/Lifecycle(644): In the onCreate() event  
  2. 03-23 01:54:32.602: D/Lifecycle(644): In the onStart() event  
  3. 03-23 01:54:32.602: D/Lifecycle(644): In the onResume() event  

5、按“返回键”,程序退出:

[plain] view plaincopy
  1. 03-23 01:58:28.307: D/Lifecycle(644): In the onPause() event  
  2. 03-23 01:58:28.762: D/Lifecycle(644): In the onStop() event  
  3. 03-23 01:58:28.837: D/Lifecycle(644): In the onDestroy() event  

6、重新进入程序:

[plain] view plaincopy
  1. 03-23 01:59:38.282: D/Lifecycle(644): In the onCreate() event  
  2. 03-23 01:59:38.292: D/Lifecycle(644): In the onStart() event  
  3. 03-23 01:59:38.302: D/Lifecycle(644): In the onResume() event  

7、按“拨号键”进入拨号界面,activity被转入后台运行:

[plain] view plaincopy
  1. 03-23 02:00:23.252: D/Lifecycle(644): In the onPause() event  
  2. 03-23 02:00:24.522: D/Lifecycle(644): In the onStop() event  

8、注意,此时onDestroy()方法并没有被触发,说明这个activity还在内存中。按“返回键”,退出拨号界面,这个Activity又重新可见了。观察LogCat窗口中的输出:

[plain] view plaincopy
  1. 03-23 02:03:25.262: D/Lifecycle(644): In the onRestart() event  
  2. 03-23 02:03:25.262: D/Lifecycle(644): In the onStart() event  
  3. 03-23 02:03:25.262: D/Lifecycle(644): In the onResume() event  


原创粉丝点击