Android之Activity生命周期


首先我们还是来看一下Android API提供的Activity生命周期图,如下图所示。可以看出,一个Activity的生命周期会经历onCreate()→onStart()→onResume()→onPause()→onStop()→onDestroy()这几个过程。不过光看图还是有点抽象,下面我们结合一个小例子来熟悉一下Activity的生命周期具体的流程。 Android之Activity生命周期_第1张图片 首先我们新建一个项目ActivityLifecycle,通过在各个继承的函数中加入log信息来跟踪程序执行的状况。第一个Activity的代码如下所示:
public class ActivityTest extends Activity{private static final String TAG="ActivityTest";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.e(TAG, "onCreate");Button btn=(Button)findViewById(R.id.button1);//监听button的事件信息btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//新建一个Intent意图对象Intent intent=new Intent();//指定intent要启动的类intent.setClass(ActivityTest.this, Activity2.class);//启动一个新的ActivitystartActivity(intent);//关闭当前的ActivityActivityTest.this.finish();}});Button btn2=(Button)findViewById(R.id.button2);//监听button的事件信息btn2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//关闭当前的ActivityActivityTest.this.finish();}});}//程序开始@Overrideprotected void onStart() {super.onStart();Log.e(TAG, "onStart");}//程序恢复@Overrideprotected void onResume() {super.onResume();Log.e(TAG, "onResume");}//程序暂停@Overrideprotected void onPause() {super.onPause();Log.e(TAG, "onPause");}//程序停止@Overrideprotected void onStop() {super.onStop();Log.e(TAG, "onStop");}//程序销毁@Overrideprotected void onDestroy() {super.onDestroy();Log.e(TAG, "onDestroy");}//程序重启@Overrideprotected void onRestart() {super.onRestart();Log.e(TAG, "onRestart");}}
第二个Activity的代码如下所示。与ActivityTest类似,在每个地方中加上一些log信息,用于记录当前运行状态。
public class Activity2 extends Activity {private static final String TAG = "Activity2";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_2);Log.e(TAG, "onCreate");Button btnStartNew = (Button) findViewById(R.id.btnStartNew);// 监听button的事件信息btnStartNew.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// 新建一个Intent对象Intent intent = new Intent();// 指定intent要启动的类intent.setClass(Activity2.this, ActivityTest.class);// 启动一个新的ActivitystartActivity(intent);// 关闭当前的ActivityActivity2.this.finish();}});Button btnExit=(Button)findViewById(R.id.btnExit);btnExit.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//关闭当前的ActivityActivity2.this.finish();}});}//程序开始@Overrideprotected void onStart() {super.onStart();Log.e(TAG, "onStart");}//程序恢复@Overrideprotected void onResume() {super.onResume();Log.e(TAG, "onResume");}//程序暂停@Overrideprotected void onPause() {super.onPause();Log.e(TAG, "onPause");}//程序停止@Overrideprotected void onStop() {super.onStop();Log.e(TAG, "onStop");}//程序销毁@Overrideprotected void onDestroy() {super.onDestroy();Log.e(TAG, "onDestroy");}//程序重启@Overrideprotected void onRestart() {super.onRestart();Log.e(TAG, "onRestart");}}
我们在布局文件中简单定义了两个Button和一个TextView,一个Button用来切换到另一个Activity,另一个Button用于关闭当前界面,TextView用来显示当前Activity的名称。第一个Activity对应的布局文件activity_main.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" >    <TextView        android:id="@+id/info"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="ActivityTest" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="启动第二个Activity" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="关闭当前Activity" /></LinearLayout>
第二个Activity对应的布局文件activity_2.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" >    <TextView        android:id="@+id/info"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="第二个Activity" />    <Button        android:id="@+id/btnStartNew"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button" />    <Button        android:id="@+id/btnExit"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button" /></LinearLayout>
然后我们要在AndroidManifest.xml文件中声明这两个Activity,如下所示:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.veione.activitytest"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="9"        android:targetSdkVersion="21" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity android:name="com.veione.activitytest.ActivityTest" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name="com.veione.activitytest.Activity2" />    </application></manifest>
最后我们运行此程序。首先我们启动程序,可以看到Activity经历了onCreate、onStart、onResume这3个函数,如图所示。 然后我们单击“启动第二个Activity”,可以看到ActivityTest显示了onPause,如图所示。接下来执行Activity中的onCreate->onStart->onResume(),然后在ActivityTest中执行了onStop->onDestroy将ActivityTest依次停止、销毁。 Android之Activity生命周期_第2张图片 我们在第二个Activity中单击"关闭",可以看到log信息如图所示,Activity依次执行了onPause->onStop->onDestroy。 自此我们整个Activity的生命周期是不是都已经掌握了呢,反正我已经掌握了。


更多相关文章

  1. Android 将自己的应用程序改成系统的应用程序
  2. 转载 Android 通过adb shell命令查看内存,CPU,启动时间,电量等信息
  3. Android 程序员指南 PDF下载
  4. Android:系统信息(内存、cpu、sd卡、电量、版本)的获取
  5. android应用 小试牛刀 开发自己的应用程序就是这么简单
  6. Google官方提供的有用的Android范例程序,值得学习的新技术
  7. Android中获取应用程序(包)的信息-----PackageManager的使用(

随机推荐

  1. Android布局的一些属性和开关、创建log图
  2. Unity与Android交互
  3. Android 软键盘 禁用回车功能或者换成搜
  4. android-1.5,1.6,2.0,2.1,2.2源码下载地
  5. Android kernel code merged into mainli
  6. Android TextView加上阴影效果
  7. Android 启动过程
  8. android 多窗口模式activity的生命周期
  9. Android获取摄像头详细信息
  10. MAC上使用maven打android的包,报错:No And