简单介绍
活动是一个应用程序组件,提供一个屏幕供用户进行交互。
返回栈
Android 是用任务来管理活动的,一个任务就是一组存放在栈里的活动的集合,这个栈称作返回栈。
活动状态
根据活动在返回栈中的位置判断活动的状态:
- 运行状态:栈顶。
- 暂停状态:非栈顶但可见。
- 停止状态:非栈顶且不可见。
- 销毁状态:不在栈中。
生命周期
拥有 7 个回调方法:
onCreate()
:活动被创建时调用。onStart()
:活动由不可见变为可见时调用。onResume()
:活动准备好与用户进行交互时调用。onPasue()
:活动停止与用户交互时调用。onStop()
:活动由可见变为不可见时调用。onDestroy()
:活动销毁时调用。onRestart()
:活动由停止状态变为运行状态时调用。
启动模式
四种启动模式:
standard
:默认的启动模式,每次启动活动都会创建一个新的实例入栈。singleTop
:启动活动时判断栈顶实例是否是该活动,若是则重用该实例,否则创建新实例入栈。singleTask
:启动活动时判断栈中是否存在该活动实例,若存在,将intent
交予处理,否则创建新实例放入一个新的返回栈,这个栈允许其它实例进入。singleInstance
:启动活动时创建一个单独的返回栈来管理该活动实例,该返回栈只有这一个活动实例,所有应用程序共享该返回栈。
通过注册文件配置启动模式:
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"> <!-- 通过 <activity> 标签的 android:launchMode 属性指定启动模式 -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
通过 Intent
设置启动模式:
Intent intent = new Intent("xxx.xxx.xxx");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 以 singleTask 模式启动指定活动
if (intent.resolveActivity(getPackageManager()) != null) { // 检查有无能够处理该 intent 的活动
startActivity(intent);
}
Flag
列出部分标识:
FLAG_ACTIVITY_NEW_TASK
,指定singleTask
启动模式。FLAG_ACTIVITY_SINGLE_TOP
,指定singleTop
启动模式。FLAG_ACTIVITY_CLEAR_TOP
,启动时,该实例上的所有实例都需要出栈。FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
,该活动不会出现在历史任务列表中。
Intent
Intent
是一个消息对象,用于在不同组件之间传递数据。
启动活动
通过 类名
启动:
Intent intent = new Intent(this, otherActivity.class); // 显式 Intent
startActivity(intent); // 启动活动
通过 action
启动:
Intent intent = new Intent("xxx.xxx.xxx"); // 被启动的 activity 在 Manifest 中注册的 action
startActivity(intent);
传递数据
发送方通过 intent
直接携带数据发送:
Intent intent = new Intent(this, otherActivity.class);
intent.putExtra("key", "value"); // 传递基本数据数据
startActivity(intent);
发送方通过 bundle
发送:
Intent intent = new Intent(this, otherActivity.class);
Bundle bundle = new Bundle();
bundle.putString("key", "value"); // 传递基本数据类型以及其他复杂的类型
intent.putExtras(bundle);
startActivity(intent);
接收方可以在 onCreate()
和 onNewIntent
方法中处理传递的 Intent
:
Intent intent = getIntent(); // 被其它组件启动接收的 intent
if (intent != null) {
String str = intent.getStringExtra("key");
// ------------
Bundle bundle = intent.getExtras();
if (bundle != null) {
xxx;
}
}
数据回传
发送方使用 startActivityForResult()
方法,并重写 onActivityResult()
方法:
Intent intent = new Intent(this, otherActivity.class);
intent.putExtra("key", "value");
startActivityForResult(intent, 101); // 携带请求码
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { // 回返时回调
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) { // 判断请求码
if (resultCode == 100) { // 判断响应码
int rValue = data.getIntExtra("key_return", 0);
Log.d("xxx", "[MainActivity.java] -- onActivityResult --rValue:" + rValue);
}
}
}
接收方使用 setResult()
方法回传数据:
Intent from = getIntent();
if (from != null) {
if (from.hasExtra("key")) {
String data = from.getStringExtra("key");
if (data != null) {
// deal with data
}
}
Intent returnIntent = new Intent();
returnIntent.putExtra("key_return", 99); // 回返数据
setResult(100, returnIntent); // 携带响应码
}