Activity 生命周期详解
生命周期,对于初学者来讲可能比较深奥。在各种面试中,常常会被问到xxx的生命周期是什么样子的,比如:Servlet的生命周期,Spring Bean的生命周期。今天就详细的讲解一下Android Activity 的生命周期。文章主要涉及到如下几个部分:生命周期图,测试代码,结果分析。
1. 生命周期图
官方有提供 Activity的 生命周期图,如下:
从上图可以清晰的看到 Activity 生命周期中涉及到的主要 方法包括: onCreate(),onStart(),onResume(),onPause(),onStop,onDestory()。 除了这一个还有 onRestart()。
前面的六个方法是相互对应的,比如onCreate 和 onDestory。
其中彩色标示的是几个常态:启动,运行,关闭,杀掉进程。
2.测试代码
光看看,永远都是抽象的。对于程序员,可用的代码才是具体的。为了验证上面的周期图和具体是如何转换的,我写了一个测试程序。程序只有两个Activity,他们之间相互切换。在这个过程中涉及到LogCat (android.util.log),测试结果主要是在这里查看的,我用的编程工具是 eclipse。具体代码如下:
LifecycleActivity1.java
package org.wpg.android.lifecycle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
public class LifecycleActivity1 extends Activity {
/** Called when the activity is first created. */
private static final String TAG = "LifecycleActivity1";
@Override
public void onCreate(Bundle savedInstanceState) {
//打开的时候调用
Log.i(TAG, "Activity1 onCreate called!");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onStart() {
//打开的时候调用
Log.i(TAG, "Activity1 onStart called!");
super.onStart();
}
@Override
protected void onRestart() {
Log.i(TAG, "Activity1 onRestart called!");
super.onRestart();
}
@Override
protected void onResume() {
//打开的时候调用
Log.i(TAG, "Activity1 onResume called!"); //在关闭屏幕开启的时候会调用
super.onResume();
}
@Override
protected void onPause() {
Log.i(TAG, "Activity1 onPause called!"); //当关闭屏幕是调用
super.onPause();
}
@Override
protected void onStop() {
Log.i(TAG, "Activity1 onStop called!");
super.onStop();
}
@Override
protected void onDestroy() {
Log.i(TAG, "Activity1 onDestroy called!");
super.onDestroy();
}
/**当点击屏幕时,进入Activity2*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Intent intent = new Intent(this, LifecycleActivity2.class);
startActivity(intent);
return super.onCreateOptionsMenu(menu);
}
}
LifecycleActivity2.java
package org.wpg.android.lifecycle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
public class LifecycleActivity2 extends Activity {
/** Called when the activity is first created. */
private static final String TAG = "LifecycleActivity2";
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "Activity2 onCreate called!");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onStart() {
Log.i(TAG, "Activity2 onStart called!");
super.onStart();
}
@Override
protected void onRestart() {
Log.i(TAG, "Activity2 onRestart called!");
super.onRestart();
}
@Override
protected void onResume() {
Log.i(TAG, "Activity2 onResume called!");
super.onResume();
}
@Override
protected void onPause() {
Log.i(TAG, "Activity2 onPause called!");
super.onPause();
}
@Override
protected void onStop() {
Log.i(TAG, "Activity2 onStop called!");
super.onStop();
}
@Override
protected void onDestroy() {
Log.i(TAG, "Activity2 onDestroy called!");
super.onDestroy();
}
/**当点击屏幕时,进入Activity2*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Intent intent = new Intent(this, LifecycleActivity1.class);
startActivity(intent);
return super.onCreateOptionsMenu(menu);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.wpg.android.lifecycle"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app1" >
<activity
android:name=".LifecycleActivity1"
android:label="@string/app1" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LifecycleActivity2"
android:label="@string/app2" >
</activity>
</application>
</manifest>
代码简单讲解:
代码非常简单,就是写了两个 activity ,覆盖了父类的几个和生命周期有关的方法,和一个主配置文件。
public boolean onCreateOptionsMenu(Menu menu) {
Intent intent = new Intent(this, LifecycleActivity1.class);
startActivity(intent);
return super.onCreateOptionsMenu(menu);
}
这个代码主要是实现,在点击手机的菜单键的时候实现 activity的切换。
3.分析
具体的测试过程以及结果如下:
a.打开应用:onCreate-》onStart-》 onResume ,在开始的时候这三个方法都会调用。
b.从第一个activity切换到第二个:先会 onPause第一个,然后onCreate-》onStart-》 onResume,最后在onStop
c.从第二个切换到第一个:内容与上一步相同
d.按手机的回退(back)键:先onPause 第一个,然后 onRestart-》 onStart-》 onResume ,然后onStop-》 onDestroy 第二个
e.按 home 键:会调用 onPause 和 onStop
f.再安home 键 重新选择程序进入:onRestart-》onStart-》onResume
g.最终退出:onPause-》 onStop》onDestroy
** e~f 不论当前状态时第一个activity 或者第二个 运行的结果都是一样的。
打开应用是显示的日志如下:
04 - 07 12 : 09 : 12.510 : I/LifecycleActivity1( 12228 ): Activity1 onCreate called! 04 - 07 12 : 09 : 12.540 : I/LifecycleActivity1( 12228 ): Activity1 onStart called! 04 - 07 12 : 09 : 12.540 : I/LifecycleActivity1( 12228 ): Activity1 onResume called! |
从第一个activity切换到第二个:
04 - 07 12 : 12 : 48.800 : I/LifecycleActivity1( 12228 ): Activity1 onPause called! 04 - 07 12 : 12 : 48.820 : I/LifecycleActivity2( 12228 ): Activity2 onCreate called! 04 - 07 12 : 12 : 48.830 : I/LifecycleActivity2( 12228 ): Activity2 onStart called! 04 - 07 12 : 12 : 48.830 : I/LifecycleActivity2( 12228 ): Activity2 onResume called! 04 - 07 12 : 12 : 49.060 : I/LifecycleActivity1( 12228 ): Activity1 onStop called! |
从第二个切换到第一个:
04 - 07 12 : 14 : 03.020 : I/LifecycleActivity2( 12228 ): Activity2 onPause called! 04 - 07 12 : 14 : 03.040 : I/LifecycleActivity1( 12228 ): Activity1 onCreate called! 04 - 07 12 : 14 : 03.050 : I/LifecycleActivity1( 12228 ): Activity1 onStart called! 04 - 07 12 : 14 : 03.050 : I/LifecycleActivity1( 12228 ): Activity1 onResume called! 04 - 07 12 : 14 : 03.290 : I/LifecycleActivity2( 12228 ): Activity2 onStop called! |
按手机的回退(back)键:
04 - 07 12 : 14 : 56.740 : I/LifecycleActivity1( 12228 ): Activity1 onPause called! 04 - 07 12 : 14 : 56.780 : I/LifecycleActivity2( 12228 ): Activity2 onRestart called! 04 - 07 12 : 14 : 56.780 : I/LifecycleActivity2( 12228 ): Activity2 onStart called! 04 - 07 12 : 14 : 56.790 : I/LifecycleActivity2( 12228 ): Activity2 onResume called! 04 - 07 12 : 14 : 57.030 : I/LifecycleActivity1( 12228 ): Activity1 onStop called! 04 - 07 12 : 14 : 57.030 : I/LifecycleActivity1( 12228 ): Activity1 onDestroy called! |
按 home 键:
04 - 07 12 : 16 : 12.730 : I/LifecycleActivity2( 12228 ): Activity2 onPause called! 04 - 07 12 : 16 : 12.950 : I/LifecycleActivity2( 12228 ): Activity2 onStop called! |
再安home 键 重新选择程序进入:
04 - 07 12 : 17 : 25.870 : I/LifecycleActivity2( 12228 ): Activity2 onRestart called! 04 - 07 12 : 17 : 25.870 : I/LifecycleActivity2( 12228 ): Activity2 onStart called! 04 - 07 12 : 17 : 25.870 : I/LifecycleActivity2( 12228 ): Activity2 onResume called! |
最终退出:
04 - 07 12 : 20 : 45.170 : I/LifecycleActivity1( 12228 ): Activity1 onPause called! 04 - 07 12 : 20 : 45.380 : I/LifecycleActivity1( 12228 ): Activity1 onStop called! 04 - 07 12 : 20 : 45.380 : I/LifecycleActivity1( 12228 ): Activity1 onDestroy called! |
关闭显示器和打开显示器也会调用相应的方法:
onPause 和 OnResume
结论:
明白了生命周期中的各个方法的执行时机,其实就相当于明白了它的生命周期。在实际开发中可以根据自己的需要,在对应 的方法里面添加相应的内容,关于这方面的技巧就由大家自己摸索,不在这里多说了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· C#高性能开发之类型系统:从 C# 7.0 到 C# 14 的类型系统演进全景
· 从零实现富文本编辑器#3-基于Delta的线性数据结构模型
· 记一次 .NET某旅行社酒店管理系统 卡死分析
· 长文讲解 MCP 和案例实战
· Hangfire Redis 实现秒级定时任务,使用 CQRS 实现动态执行代码
· C#高性能开发之类型系统:从 C# 7.0 到 C# 14 的类型系统演进全景
· 管理100个小程序-很难吗
· 基于Blazor实现的运输信息管理系统
· 如何统计不同电话号码的个数?—位图法
· 聊聊四种实时通信技术:长轮询、短轮询、WebSocket 和 SSE