---
title: "Android笔记(5)---Activity生命周期"
author: "ln_ydc"
date: "2013-07-21T14:06:50Z"
tags: []
categories: ["移动开发"]
slug: "android-5-activity"
draft: false
lastmod: "2013-07-21T14:12:59Z"
id: 1911072
url: "https://ln-ydc.iteye.com/blog/1911072"
---

Android api中提供的Activity生命周期图

![](http://dl2.iteye.com/upload/attachment/0087/2309/8846ed62-2312-34a5-a6f5-656341e2158b.png)  
 

创建一个测试项目ActivityLife

在MainActivity.java中

重写onCreate,onStart,onResume,onPause,onStop,onRestart,onDestroy方法

 

```
package com.example.activitylife;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.e(TAG, "~~~onCreate~~~");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "~~~onStart~~~");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e(TAG, "~~~onReStart~~~");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "~~~onResume~~~");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "~~~onPause~~~");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "~~~onStop~~~");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "~~~onDestroy~~~");
    }
}
```

 

运行该应用程序

下面是不同操作对应的LogCat输出

打开应用程序时，对应下图过程1  
![](http://dl2.iteye.com/upload/attachment/0087/2311/8fbd5d6a-d615-3503-8617-55e38ff6fa8f.png)  
 

打开应用程序后，按下BACK键时，对应下图过程2  
![](http://dl2.iteye.com/upload/attachment/0087/2313/0d2fdffb-36d1-3b27-9184-5f9d76365f1e.png)  
 

打开应用程序后，按下HOME键时，对应下图过程3

![](http://dl2.iteye.com/upload/attachment/0087/2315/2e0541f7-5eb2-38ff-8ed5-56ad3ec0acc8.png)  
 

在上一操作的基础上，再次打开应用程序时，对应下图过程4

![](http://dl2.iteye.com/upload/attachment/0087/2317/6a6065b7-12fd-3db1-9226-8ebb087bc51b.png)

操作流程图：

![](http://dl2.iteye.com/upload/attachment/0087/2319/c648debe-c432-39f6-a56a-61b9ca8adc9c.png)  
 ========================================================================

参考文章提出的一个问题：

EditText在状态1时输入一个字符串,如"Hello,Android!",再经过3，4状态后无法保持

看到有评论说  有id的view，android会自动维护状态

经测试，无论EditText无论有无id,字符串都可保持住

测试如下：

修改res/layout/activity\_main.xml，在布局中添加一个EditText控件

```
<? xml version ="1.0" encoding= "utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
    xmlns:tools ="http://schemas.android.com/tools"
    android:layout_width ="fill_parent"
    android:layout_height ="fill_parent"
    android:orientation ="vertical"
    android:paddingBottom ="@dimen/activity_vertical_margin"
    android:paddingLeft ="@dimen/activity_horizontal_margin"
    android:paddingRight ="@dimen/activity_horizontal_margin"
    android:paddingTop ="@dimen/activity_vertical_margin"
    tools:context =".MainActivity" >

    <TextView
        android:layout_width ="fill_parent"
        android:layout_height ="wrap_content"
        android:text ="@string/hello_world" />

    <EditText
        android:id ="@+id/editText"
        android:layout_width ="fill_parent"
        android:layout_height ="wrap_content"
        android:inputType ="text" />

</ LinearLayout>
```

 

再依次运行过程1--&gt;3--&gt;4

在状态1的时候，在EditText控件中写入一个字符串，如Hello,Android!，在经过3，4过程后字符串依旧保持  
![](http://dl2.iteye.com/upload/attachment/0087/2321/7eba93ef-583c-382d-a2cb-787937c1277f.png)  
  ========================================================================

测试状态5

修改res/layout/main\_activity.xml，添加一个Button

```
<? xml version ="1.0" encoding= "utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
    xmlns:tools ="http://schemas.android.com/tools"
    android:layout_width ="fill_parent"
    android:layout_height ="fill_parent"
    android:orientation ="vertical"
    android:paddingBottom ="@dimen/activity_vertical_margin"
    android:paddingLeft ="@dimen/activity_horizontal_margin"
    android:paddingRight ="@dimen/activity_horizontal_margin"
    android:paddingTop ="@dimen/activity_vertical_margin"
    tools:context =".MainActivity" >

    <TextView
        android:id = "@+id/mainTv"
        android:layout_width ="fill_parent"
        android:layout_height ="wrap_content"
        android:text ="@string/hello_world" />

    <EditText
        android:id = "@+id/mainEt"
        android:layout_width= "fill_parent"
        android:layout_height ="wrap_content"
        android:inputType ="text" />

    <Button
        android:id ="@+id/callDialog"
        android:layout_width ="fill_parent"
        android:layout_height ="wrap_content"
        android:text ="@string/call_dialog" />

</ LinearLayout>
```

 

添加一个Activity，SecondActivity

并重写onCreate,onStart,onResume,onPause,onStop,onRestart,onDestroy方法

```
package com.example.activitylife;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class SecondActivity extends Activity {

    private static final String TAG = "SecondActivity";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        
        Log.e(TAG, "~~~onCreate~~~");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "~~~onStart~~~");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e(TAG, "~~~onReStart~~~");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "~~~onResume~~~");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "~~~onPause~~~");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "~~~onStop~~~");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "~~~onDestroy~~~");
    }
}
```

 

修改MainActivity.java

```
package com.example.activitylife;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";
    private Button callDialogBut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.e(TAG, "~~~onCreate~~~");

        callDialogBut = (Button) findViewById(R.id.callDialog);
        callDialogBut.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "~~~onStart~~~");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e(TAG, "~~~onReStart~~~");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "~~~onResume~~~");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "~~~onPause~~~");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "~~~onStop~~~");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "~~~onDestroy~~~");
    }
}
```

 

修改AndroidManifest.xml文件

```
<? xml version ="1.0" encoding= "utf-8" ?>
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
    package ="com.example.activitylife"
    android:versionCode ="1"
    android:versionName ="1.0" >

    <uses-sdk
        android:minSdkVersion ="8"
        android:targetSdkVersion ="17" />

    <application
        android:allowBackup ="true"
        android:icon ="@drawable/ic_launcher"
        android:label ="@string/app_name"
        android:theme ="@style/AppTheme" >
        < activity
            android:name ="com.example.activitylife.MainActivity"
            android:label ="@string/app_name" >
            < intent-filter>
                < action android:name ="android.intent.action.MAIN" />

                < category android:name ="android.intent.category.LAUNCHER" />
            </ intent-filter>
        </ activity>
        < activity
            android:name =".SecondActivity"
            android:label ="@string/app_name"
            android:theme ="@android:style/Theme.Dialog" >
        </ activity>
    </application >

</ manifest>
```

 

启动应用程序后，点击 调用对话框 按钮，相当于过程5中的onPause  
![](http://dl2.iteye.com/upload/attachment/0087/2323/474b927f-ba8d-331f-8bf5-7b3af3014501.png)  
 

在上一步操作的基础上，点击BACK键，相当于过程5中的onResume  
![](http://dl2.iteye.com/upload/attachment/0087/2325/fec5e049-30f6-3b43-a8eb-1aa231915aed.png)  
 

程序运行截图：

启动应用程序  
![](http://dl2.iteye.com/upload/attachment/0087/2327/cdad97ad-cc3d-3c40-a910-32607cf64692.png)

 

按下 调用对话框 按钮  
![](http://dl2.iteye.com/upload/attachment/0087/2329/2aace72b-d3f6-3d6d-a418-845d5f7f6a24.png)  
 

再按下BACKUP键  
![](http://dl2.iteye.com/upload/attachment/0087/2331/473f2472-13c6-3356-9a91-cf9745b46d9f.png)  
 

 

 

参考文章：

\-----------------------

[两分钟彻底让你明白Android Activity生命周期(图文)!](http://blog.csdn.net/android_tutor/article/details/5772285)