Activity的onNewIntent方法究竟再什么时候 执行。
据Android API文档,有如下描述:
据API描述,
大概意思是当Activity被设以singleTop启动,当需要再次响应此Activity启动需求时,会复用栈顶的已有Activity,还会调用onNewIntent方法。
并且,再接受新发送来的intent(onNewIntent方法)之前,一定会先执行onPause方法。
配合一个demo来理解此描述:NfcCardRead。
接下来将结合操作步骤和log来分布描述:
1.
由于demo主Activity(MainActivity)在manifest中注册了NfcAdapter.ACTION_TECH_DISCOVERED,可以响应读取某类Tag。
简言之,此demo可以响应刷取某类公交卡,通过刷公交卡,可以直接调起本demo主Activity。
首先,我们进行第一次刷卡,调起主Activity,我们的焦点从launcher转移至MainActivity中,此时Activity对应的生命周期回调为onResume.
07-20 16:49:24.842 1587 2237 V WindowManager: findFocusedWindow: Found new focus @ 2 = Window{eb0dcc2 u0 com.tct.launcher/com.tct.launcher.Launcher}
//第一次刷卡启动应用
07-20 16:49:28.248 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity onCreate().
07-20 16:49:28.814 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity onStart().
07-20 16:49:28.817 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity onResume(). enableForegroundDispatch
07-20 16:49:29.034 1587 1615 V WindowManager: findFocusedWindow: Found new focus @ 3 = Window{ae91d5b u0 com.knealq.user.nfccardread/com.knealq.user.nfccardread.MainActivity}
07-20 16:51:11.830 1587 2633 V WindowManager: findFocusedWindow: Found new focus @ 3 = Window{ae91d5b u0 com.knealq.user.nfccardread/com.knealq.user.nfccardread.MainActivity}
//隔断时间再次刷卡
07-20 16:51:11.850 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity onPause(). disableForegroundDispatch
07-20 16:51:11.857 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity onNewIntent().
07-20 16:51:11.895 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity showDataText() : null
07-20 16:51:11.895 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity showHintText().
07-20 16:51:11.921 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity onResume(). enableForegroundDispatch//界面信息更新后,再次回到onResume
07-20 16:51:11.928 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity showHintText().
07-20 16:51:24.168 1587 1999 V WindowManager: findFocusedWindow: Found new focus @ 1 = Window{ae91d5b u0 com.knealq.user.nfccardread/com.knealq.user.nfccardread.MainActivity}
//按下Home键回到launcher
07-20 16:51:24.249 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity onPause(). disableForegroundDispatch
07-20 16:51:24.334 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity onStop().//位于launcher
07-20 16:51:24.502 1587 2098 V WindowManager: findFocusedWindow: Found new focus @ 3 = Window{eb0dcc2 u0 com.tct.launcher/com.tct.launcher.Launcher}
07-20 16:51:24.940 1587 1627 V WindowManager: findFocusedWindow: Found new focus @ 3 = Window{eb0dcc2 u0 com.tct.launcher/com.tct.launcher.Launcher}
//再次刷卡把MainActivity调起成为前台界面
07-20 16:51:30.902 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity onNewIntent().
07-20 16:51:30.910 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity showDataText() : null
07-20 16:51:30.911 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity showHintText().
07-20 16:51:30.933 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity onRestart().
07-20 16:51:30.939 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity onStart().
07-20 16:51:30.940 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity onResume(). enableForegroundDispatch
07-20 16:51:30.947 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity showHintText().
07-20 16:51:31.023 1587 2236 V WindowManager: findFocusedWindow: Found new focus @ 3 = Window{ae91d5b u0 com.knealq.user.nfccardread/com.knealq.user.nfccardread.MainActivity}
07-20 16:52:11.998 1587 2669 V WindowManager: findFocusedWindow: Found new focus @ 3 = Window{ae91d5b u0 com.knealq.user.nfccardread/com.knealq.user.nfccardread.MainActivity}
//待16:52再次刷卡响应
07-20 16:52:12.013 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity onPause(). disableForegroundDispatch
07-20 16:52:12.018 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity onNewIntent().
07-20 16:52:12.040 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity showDataText() : null
07-20 16:52:12.040 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity showHintText().
07-20 16:52:12.083 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity onResume(). enableForegroundDispatch
07-20 16:52:12.105 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity showHintText().
07-20 16:52:36.143 1587 2237 V WindowManager: findFocusedWindow: Found new focus @ 1 = Window{ae91d5b u0 com.knealq.user.nfccardread/com.knealq.user.nfccardread.MainActivity}
//按下back键,退回launcher
07-20 16:52:36.286 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity onPause(). disableForegroundDispatch
07-20 16:52:36.550 1587 2239 V WindowManager: findFocusedWindow: Found new focus @ 3 = Window{eb0dcc2 u0 com.tct.launcher/com.tct.launcher.Launcher}
07-20 16:52:36.931 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity onStop().
//按下recent键,清除应用栈
07-20 16:52:36.940 22823 22823 E NfcReadCard-MainActivity: NfcCardRead MainActivity onDestroy().
07-20 16:52:36.972 1587 2234 V WindowManager: findFocusedWindow: Found new focus @ 3 = Window{eb0dcc2 u0 com.tct.launcher/com.tct.launcher.Launcher}
从上述流程来看,每次执行onNewIntent之前,一定有经历过onPause状态。两次回到launcher的流程如下:
第1次
onCreate -> onStart -> onResume ->读卡 -> onPause -> onNewIntent() ->onResume -> onPause ->onStop -> 回到Launcher
第2次,接上次状态
Launcher| onStop ->读卡 -> onNewIntent() -> onRestart -> onStart -> onResume -> 读卡 -> 接第一次读卡后流程 ->onDestroy
因此,onNewIntent有可能在以下两个地方执行
附
完整的log可见链接:
http://download.csdn.net/detail/u013398960/9889776