Android Activity生命周期


 Activity与用户的操作息息相关,在用户看来Activity就是一个当前全屏的窗口 ,开发人员可以通过setContentView(View)方法设置Activity的UI。

 Activity被android系统以Activity栈的方式管理,一个新启动的Activity被置于栈顶并处于运行状态,相当于入栈操作,上一个  Activity依然保留,在栈中位于新Activity的下方。

andorid提供了以下的方法供重写,每个方法对应着一个Activity的状态

public class Activity extends ApplicationContext {
     protected void onCreate(Bundle savedInstanceState);
     protected void onStart();    

     protected void onRestart();
     protected void onResume();
     protected void onPause();
     protected void onStop();
     protected void onDestroy();
 }

下面一张图说明了Activity的生命周期。

Android Activity生命周期_第1张图片

 


Activity启动后,onCreate()方法被执行,此方法一般在新打开一个Activity序后执行一次,可以把初始化的操作放到这个方法里面。然后是onstart(),onResume()。此时Activity展现到屏幕最前面,进入运行状态。如果当前Activity失去焦点但是没有完全不可见时(被其他Activity部分覆盖),进入暂停状态,执行onPause()方法,此时结束部分覆盖它的Activity,则重新回到运行状态,执行onResume()方法。如果Activity不可见,即被其他Activity完全覆盖时,进入停止状态,执行onStop()方法,如果此时结束完全覆盖它的Activity,则执行onRestart()->onStart()->onResume()方法,重新回到运行状态。当选择结束当前Activity时,执行onPause()->onStop()->onDestroy()方法。结束Activity有2种方式,一种是通过finish()方法主动关闭,另外一种是通过android系统自动回收,前面提到,Activity是通过栈的方式管理的,内存不足时,如果有优先级更高的Activity要运行,则系统会回收之前的一个Activity,用户重新选择被回收的Activity时,又重新执行一次onCreate()方法。

下面通过一个例子验证上面的流程。

工程结构:

Android Activity生命周期_第2张图片

清单文件:

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

    <uses-sdk android:minSdkVersion="9" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ActivityLifeActivity"
            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=".OtherActivity"></activity>
        <!-- dialog风格的Activity,没有完全覆盖当前Activity -->
        <activity android:name=".PauseActivity" android:theme="@android:style/Theme.Dialog"></activity>
    </application>

</manifest>
布局文件:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
	<Button
	    android:id="@+id/dialog"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:text="OnPause"
	    />
	<Button
	    android:id="@+id/activity"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:text="Onstop"
	    />
</LinearLayout>

other.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</LinearLayout>

主Activity:这里用log打印输出

package test.activitylife;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
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 ActivityLifeActivity extends Activity {
	
	private Button onPause;
	private Button onStop;
	private static final String TAG = "MainActivity";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViews();
        onPause.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(ActivityLifeActivity.this, PauseActivity.class);
				startActivity(intent);
			}
        	
        });
        
        onStop.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				Intent intent = new Intent(ActivityLifeActivity.this, OtherActivity.class);
				startActivity(intent);
			}
        	
        });
        Log.v(TAG, "---------------onCreate--------------------");
    }

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

	@Override
	protected void onRestart() {
		Log.v(TAG, "---------------onRestart--------------------");
		super.onRestart();
	}

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

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

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

	@Override
	protected void onDestroy() {
		Log.v(TAG, "---------------onDestroy--------------------");
		super.onDestroy();
	}
	
	public void findViews() {
		onPause = (Button)findViewById(R.id.dialog);
		onStop = (Button)findViewById(R.id.activity);
	}
    
}

两个用于测试的Activity:

otherActivity

package test.activitylife;

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

public class OtherActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
	}
	
}

PauseActivity

package test.activitylife;

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

public class PauseActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other);
	}

}

启动应用程序 Logcat输出:


Android Activity生命周期_第3张图片

点击第一个按钮,启动一个不完全覆盖它的Activity

Android Activity生命周期_第4张图片


点击返回键


点击第二个按钮,启动一个完全覆盖它的Activity(或者按home键返回桌面,Logcat输出是一样的)


点击返回键(或者再按一次home键回到Activity)


再次点击返回键退出




你可能感兴趣的:(Android Activity生命周期)