Android之Activity生命周期


首先我们还是来看一下Android API提供的Activity生命周期图,如下图所示。可以看出,一个Activity的生命周期会经历onCreate()→onStart()→onResume()→onPause()→onStop()→onDestroy()这几个过程。不过光看图还是有点抽象,下面我们结合一个小例子来熟悉一下Activity的生命周期具体的流程。 首先我们新建一个项目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依次停止、销毁。 我们在第二个Activity中单击"关闭",可以看到log信息如图所示,Activity依次执行了onPause->onStop->onDestroy。 自此我们整个Activity的生命周期是不是都已经掌握了呢,反正我已经掌握了。


更多相关文章

  1. Android7关闭selinux(设置为Permissive模式)
  2. 转载 Android(安卓)通过adb shell命令查看内存,CPU,启动时间,电量等
  3. 访问Android硬件资源の管理网络和Wifi连接
  4. Android实现侧拉DrawerLayout简单用法
  5. Android(安卓)Studio启动时出现unable to access android sdk ad
  6. Android输入管理服务启动过程源码分析
  7. Android(安卓)APN的设置问题 默认“已起用数据” 关闭
  8. Android(安卓)实现首次开机启动wizardsetup程序
  9. Android(安卓)IntentService使用

随机推荐

  1. Windows任务管理器远比想象中的复杂
  2. antirez辞去Redis项目领导者职务
  3. 源码解析容器底层cgroup的实现
  4. 修改MySQL表varchar类型 字段长度锁表情
  5. “苹果,请送Linus Torvalds一台ARM电脑”
  6. Win 10开始采用Windows Update分发WSL2
  7. Nginx配置文件参数详解
  8. Ubuntu因在MOTD植入广告遭批评
  9. Raspberry Pi 4发布8G版,同时带来64位镜像
  10. 慕尼黑们拥抱Linux,微软一直在被抛弃的路