一.摘要
一个完整的Activity的生命周期有6个回调函数。
//创建Activity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } //Activity快要变成可见的 @Override protected void onStart() { super.onStart(); } //Activity变成可见的,处于运行状态 @Override protected void onResume() { super.onResume(); } //其他Activity获得用户焦点,(Activity快要暂停了) @Override protected void onPause() { super.onPause(); } //Activity不再可见,处于停止状态 @Override protected void onStop() { super.onStop(); } //Activity快要被销毁了 @Override protected void onDestroy() { super.onDestroy(); } }
下面这张图很好的讲解了Activity的生命周期和上面说的三种生命期:
二. 实例
下面通过一个Demo来学习以下Activity的生命周期:Demo很简单,一共三个Activity,MainActivity和NormalActivity和DialogActivity。MainActivity里面有两个按钮,分别可以跳转到NormalActivity和DialogActivity。NormalActivity和DialogActivity的唯一区别就是DialogActivity指定了Activity的主题为Dialog,即在AndroidManifest文件里面注册后者多了一句
android:theme="@android:style/Theme.Dialog"
MainActivity:
public class MainActivity extends Activity { public static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "MainActivity_onCreate"); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); Button startNormalActivity = (Button) findViewById(R.id.start_normal_button); Button startDialogActivity = (Button) findViewById(R.id.start_dialog_button); startNormalActivity.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, NormalActivity.class); startActivity(intent); } }); startDialogActivity.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, DialogActivity.class); startActivity(intent); } }); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "MainActivity_onStart"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "MainActivity_onResume"); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "MainActivity_onPause"); } @Override protected void onStop() { super.onStop(); Log.d(TAG, "MainActivity_onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "MainActivity_onDestroy"); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "MainActivity_onRestart"); } }
NormalActivity:
public class NormalActivity extends Activity { public static final String TAG = "NormalActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG,"NormalActivity_onCreate"); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.normal_layout); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "NormalActivity_onStart"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "NormalActivity_onResume"); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "NormalActivity_onPause"); } @Override protected void onStop() { super.onStop(); Log.d(TAG, "NormalActivity_onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "NormalActivity_onDestroy"); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "NormalActivity_onRestart"); } }
DialogActivity:
package com.example.haisun.activitylifecycle; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Window; /** * Created by HaiSun on 2015/10/1. */ public class DialogActivity extends Activity { public static final String TAG = "DialogActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG,"DialogActivity_onCreate"); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.dialog_layout); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "NDialogActivity_onStart"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "DialogActivity_onResume"); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "DialogActivity_onPause"); } @Override protected void onStop() { super.onStop(); Log.d(TAG, "DialogActivity_onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "DialogActivity_onDestroy"); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "DialogActivity_onRestart"); } }
AndroidManifest 文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.haisun.activitylifecycle" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".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=".NormalActivity"> </activity> <activity android:name=".DialogActivity" android:theme="@android:style/Theme.Dialog"> </activity> </application> </manifest>
通过运行应用,可以观察到回调函数的调用情况打印记录:
1.运行MainActivity
可以看出一个Activity运行调用的回调方法是:onCreate()->onStart()->onResume()
2.点击按钮跳转到NormalActivity
对于MainActivity,由于不可见,状态由运行变为停止。 依次调用了它的回调方法:onPause()->onStop(),NormalActivity正常启动,调用的回调方法是:onCreate()->onStart()->onResume()。
3.点击按钮跳转到DialogActivity
由于MainActivity依然可见,所以只调用了它的onPause()方法。其他跟NormalActivity一样。
4.Activity处于运行状态,旋转屏幕
可以发现旋转屏幕是一个销毁Activity然后重新创建Activity的过程。依次调用回调方法:onPause()->onStop->onDestroy()->onCreate()->onStart()->onResume()。
注:本文写作参考了http://www.cnblogs.com/JohnTsai的相关技术博客