Android Activity 的生命周期(Life Cycle)

这是一个认识Android应用程式生命周期的练习. 通过这个练习知道,原来按下HOME按钮离开应用程式,和按下BACK按钮或调用finish()方法离开应用程式是不相同的.

按下HOME按钮离开应用程式,会顺序调用: OnPause() -> OnStop(). 再次进入应用程式会调用: OnReStart() -> OnStart() -> OnResume(). 

按下BACK按钮或调用finish()方法离开应用程式: OnPause() -> OnStop() -> onDestroy(). 再次进入应用程式会调用: OnCreate() -> OnStart() -> OnResume().


布局代码如下:

View Code
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2    android:orientation="vertical"
 3    android:layout_width="fill_parent"
 4    android:layout_height="fill_parent"
 5    >
 6 <TextView
 7    android:layout_width="fill_parent"
 8    android:layout_height="wrap_content"
 9    android:text="@string/hello"
10    />
11 <Button
12    android:id="@+id/finish"
13    android:layout_width="fill_parent"
14    android:layout_height="wrap_content"
15    android:text="exit App by finish()"
16    />
17 </LinearLayout>

后台代码如下:

View Code
 1 package com.example.helloworld;
 2 import android.app.Activity;
 3 import android.os.Bundle;
 4 import android.view.View;
 5 import android.widget.Button;
 6 import android.widget.Toast;
 7  
 8 public class MainActivity extends Activity {
 9    /** Called when the activity is first created. */
10    @Override
11    public void onCreate(Bundle savedInstanceState) {
12        super.onCreate(savedInstanceState);
13        setContentView(R.layout.activity_main);
14        
15        Button finish = (Button)findViewById(R.id.finish);
16        /*finish.setOnClickListener(new Button.OnClickListener()
17        {
18 
19         public void onClick(View v) {
20             // TODO Auto-generated method stub
21             
22         }
23            
24        });*/
25        finish.setOnClickListener(new Button.OnClickListener(){
26  
27    public void onClick(View v) {
28     // TODO Auto-generated method stub
29     finish();
30     Toast.makeText(MainActivity.this, "onClick", Toast.LENGTH_LONG);
31    }});
32        
33        Toast.makeText(this, "onCreate", Toast.LENGTH_LONG).show();
34        
35    }
36  
37  @Override
38  protected void onDestroy() {
39   // TODO Auto-generated method stub
40   super.onDestroy();
41   Toast.makeText(this, "onDestroy", Toast.LENGTH_LONG).show();
42  }
43  
44  @Override
45  protected void onPause() {
46   // TODO Auto-generated method stub
47   super.onPause();
48   Toast.makeText(this, "onPause", Toast.LENGTH_LONG).show();
49  }
50  
51  @Override
52  protected void onRestart() {
53   // TODO Auto-generated method stub
54   super.onRestart();
55   Toast.makeText(this, "onRestart", Toast.LENGTH_LONG).show();
56  }
57  
58  @Override
59  protected void onResume() {
60   // TODO Auto-generated method stub
61   super.onResume();
62   Toast.makeText(this, "onResume", Toast.LENGTH_LONG).show();
63  }
64  
65  @Override
66  protected void onStart() {
67   // TODO Auto-generated method stub
68   super.onStart();
69   Toast.makeText(this, "onStart", Toast.LENGTH_LONG).show();
70  }
71  
72  @Override
73  protected void onStop() {
74   // TODO Auto-generated method stub
75   super.onStop();
76   Toast.makeText(this, "onStop", Toast.LENGTH_LONG).show();
77  }
78 }

Android Activity的生命周期图如下:

原文地址:https://www.cnblogs.com/JailBreak02/p/2799850.html