《Android 艺术探索》第 1 章 Activity 的生命

2020-07-28  本文已影响0人  Coralline_xss

一、Activity 生命周期

  1. 当前 Activity A 打开一个新的 Activity B,生命周期调用顺序是怎样的?
    — 顺序为:A.onPause() -> B.onCreate() -> B.onStart() -> B.onResume -> A.onStop() -> A.onDestroy()
    注意点:
  1. onStart() 和 onResume()、onPause() 和 onStop() 从描述上差不多,但是有什么实质性的不同呢?
    — onStart() 和 onStop() 是从 Activity 是否可见这个角度来回调的;onResume() 和 onPause() 是从 Activity 是否位于前台这个角度来回调的,除此区别,在实际使用种无甚区别。

  2. onSaveInstanceState() 方法仅在 Activity 被异常杀掉才会调用吗?onRestoreInstanceState() 在什么时候会被调用?参数 Bundle 有什么作用?Activity 异常被杀死重新恢复后,页面上有些控件的状态会丢失,有些会保留,怎么处理的?
    — TODO 待验证 。

二、Activity 启动模式

  1. 如何理解 “任务栈” 这个概念?
  1. 四种启动模式含义及使用场景。

注:

问题:

  1. 前台任务栈 和 后台任务栈的区别。
    — 前台任务栈就是当前展示的 Activity 所在的任务栈;后台任务栈就是位于后台的 Activity 所在的任务栈。

  2. Activity 的 Flags 用法。

三、IntentFilter 的匹配规则

  1. 隐式和显式启动 Activity 。
  1. 如何通过隐式调用启动一个 Activity ?
  1. 如何通过 scheme 启动另外一个应用 / 另外一个应用的某个页面?
    方式一:通过 Intent 指定组件 包名和 类名即可。
Intent intent = new Intent(Intent.ACTION_MAIN); 
intent.addCategory(Intent.CATEGORY_LAUNCHER);             
ComponentName cn = new ComponentName(packageName, className);             
intent.setComponent(cn); 
startActivity(intent);

// 打开某个页面,则可直接指定 action 并且需要翻开对外访问权限:exported=true 。

方式二:通过 data 配置 scheme uri 进行跳转并可传递参数。

// 应用 A 启动应用 B
Uri uri = Uri.parse("bocmcht://payresult/mobile?jsonData=123");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

// 应用 B 清单文件声明
<intent-filter>
         <action android:name="android.intent.action.VIEW"/>
         <category android:name="android.intent.category.DEFAULT"/>
         <data android:scheme="bocmcht" android:host="payresult" android:path="/mobile"/>
</intent-filter>

// 应用 B 获取应用 A 传递的参数
Uri uri = getIntent().getData();
String scheme = uri.getScheme();// 打印:bocmcht
String host = uri.getHost();//打印:payresult
String path = uri.getPath();//打印:/mobile
String queryString = uri.getQuery();//打印:jsonData=123
  1. H5 如何通过 scheme 打开一个 Native 页面?
    — 同上。action 需要设置为 "android.intent.action.VIEW" 才行,并且要配置 category,如下:
<!--scheme 允许在浏览器中打开-->
<category android:name="android.intent.category.BROWSABLE"/>
上一篇 下一篇

猜你喜欢

热点阅读