Activity
是Android
最基本的四大组件之一(Activity
活动,Service
服务,ContentProvider
内容提供者,BroadcastReceiver
广播),Activity
主要负责与用户进行交互,是每位Android
开发必须掌握的知识点。
本篇文章主要介绍 Android
开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:
- Activity 生命周期 简介
- Activity 必须在AndroidMainfest.xml 中注册
- 启动Activity 的方法
- 启动带返回值的Activity
- Activity结束 方法
- Activity状态保存,恢复的方法
- 面试中经常问到的例子
欢迎关注微信公众号:程序员Android
公众号ID:ProgramAndroid
获取更多信息

我们不是牛逼的程序员,我们只是程序开发中的垫脚石。
我们不发送红包,我们只是红包的搬运工。
1. Activity 生命周期 简介
首先我们需要了解一下Activity
的继承关系。
Activity 继承关系
Activity
继承关系如下:
java.lang.Object
↳ android.content.Context
↳ android.content.ContextWrapper
↳ android.view.ContextThemeWrapper
↳ android.app.Activity
理解完Activity
的继承关系后,我们开始了解Activity
的声明周期,Activity
的生命周期直接影响到与用户的交互,此声明周期很重要。
Activity 生命周期
Activity
生命周期图如下:

在代码中 Activity
生命周期回调方法
Activity
生命周期回调方法如下:

Activity 4 种 生命周期状态
Activity 常见的四种生命周期状态如下:
- Active 运行状态
- Pause 暂停状态
- Stop 停止状态
- Killed 消亡状态
2. Activity 必须在 AndroidMainfest.xml 中注册
Activity
是四大组件之一,Android
规定四大组件必须在AndroidMainfest.xml
中注册,Activity
如果不注册,则会引起 App Crash
报错。
ActivityNotFoundException
如Activity
不在AndroidMainfest.xml
中注册,则app将会报错,报错主要异常信息如下 :
ActivityNotFoundException
例如以下报错信息:
//提示未在 AndroidMainfest.xml 中找到Activity类的声明
android.content.ActivityNotFoundException:Unable to find explicit activity class
//具体类名,包名如下:
{com.wj.utils/com.wj.utils.basewidget.BaseButtonMethods}; have you declared this activity in your AndroidManifest.xml?

Activity 注册方法如下:
<manifest ... >
<application ... >
<activity android:name=".BaseButtonMethods" />
...
</application ... >
...
</manifest >
常用Action 标签
android.action.MAIN
:
表示是当前App
的入口 main
方法类。
android.intent.category.LAUNCHER
:
表示 此Action
会被Launcher
扫描到,可以显示在Launcher
的Icon
列表上,如果去掉此Action
,则无法在Launcher
种查看到此app
的Icon
。
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
3. 启动Activity 的方法
显示启动
Intent intent = new Intent(ActivityMethods.this, OtherActivity.class)
startActivity(intent);
隐式启动
Intent intent = new Intent("string_action");
// intent.setAction("string_action");
startActivity(intent);
4. 启动带返回值的Activity
启动带返回值的Activity方法
Intent intent = new Intent();
intent.setClass(ActivityMethods.this, OtherActivity.class);
startActivityForResult(intent, mRequestCode);
处理返回值的回调方法
// 获取 Activity 返回结果的方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == mRequestCode && resultCode == mResultCode) {
String result = data.getStringExtra("str_set_result");
Toast.makeText(this, "result :" + result, Toast.LENGTH_SHORT).show();
}
}
被启动Activity设置返回值的方法
int resultCode = 101;
Intent intent = new Intent();
intent.putExtra("str_set_result", "带返回结果的Activity");
setResult(resultCode, intent);
5. Activity结束 方法
如果想结束掉当前Activity ,可以调用一下方法
finish();
//或者根据请求码结束Activity
finishActivity(int requestCode);
6. Activity状态保存,恢复的方法
当Activity
异常退出时候,Activity
会自动保存一些数据,但是,如果是app
重要数据,还请在代码中手动保存Bundle
类型的数据,防止Activity
保存的数据不理想。
Activity 状态保存与恢复
Activity 状态保存与恢复的周期图如下:

Activity 状态保存与恢复的回调方法
Activity 状态保存与恢复的回调方法如下:
// Activity 恢复数据的方法,经常在 oncreate 方法中恢复数据
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG, "----onRestoreInstanceState----");
}
// Activity 保存数据的方法,经常在 onPause 方法中保存数据
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
Log.i(TAG, "----onSaveInstanceState----");
}
7. 面试中经常问到的例子
Activity A 启动 Activity B, 然后再返回A,简述一下其生命周期?
- 首先 A 启动的声明周期如下:
01-02 21:25:22.357 21225-21225/com.android.program.programandroid I/ActivityMethods wjwj:: ----onCreate----
01-02 21:25:22.396 21225-21225/com.android.program.programandroid I/ActivityMethods wjwj:: ----onStart----
01-02 21:25:22.402 21225-21225/com.android.program.programandroid I/ActivityMethods wjwj:: ----onResume----
- 点击A中的Button ,跳转到B,此时声明周期关系如下:
01-02 21:28:30.617 23845-23845/com.android.program.programandroid I/ActivityMethods wjwj:: ----onPause----
01-02 21:28:30.723 23845-23845/com.android.program.programandroid I/OtherActivity wjwj:: ----onCreate----
01-02 21:28:30.729 23845-23845/com.android.program.programandroid I/OtherActivity wjwj:: ----onStart----
01-02 21:28:30.738 23845-23845/com.android.program.programandroid I/OtherActivity wjwj:: ----onResume----
01-02 21:28:31.320 23845-23845/com.android.program.programandroid I/ActivityMethods wjwj:: ----onStop----
- 结束 B,返回 A ,生命周期如下:
01-02 21:29:38.646 23845-23845/com.android.program.programandroid I/OtherActivity wjwj:: ----onPause----
01-02 21:29:38.668 23845-23845/com.android.program.programandroid I/ActivityMethods wjwj:: ----onRestart----
01-02 21:29:38.672 23845-23845/com.android.program.programandroid I/ActivityMethods wjwj:: ----onStart----
01-02 21:29:38.674 23845-23845/com.android.program.programandroid I/ActivityMethods wjwj:: ----onResume----
01-02 21:29:39.058 23845-23845/com.android.program.programandroid I/OtherActivity wjwj:: ----onStop----
01-02 21:29:39.059 23845-23845/com.android.program.programandroid I/OtherActivity wjwj:: ----onDestroy----
总结:
Activity A 启动 Activity B, 简述一下其生命周期? 大致流程如下图:

至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。期待您的关注,
感谢您的阅读,谢谢!
欢迎关注微信公众号:程序员Android
公众号ID:ProgramAndroid
获取更多信息

我们不是牛逼的程序员,我们只是程序开发中的垫脚石。
我们不发送红包,我们只是红包的搬运工。

点击阅读原文,获取更多福利

网友评论