Activity的onNewIntent方法执行时间

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}

2.隔了大概1分多钟,刷第二次卡,主Activity响应读卡事件,更新界面信息,此时生命周期发生了变化,从先onPause,再回到onResume,获取焦点的仍然为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}

3.按下Home键,回到launcher,MainActivity失去焦点。回到launcher,Activity为不可见,但并未destroy。

//按下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}

4.再次刷卡,调起MainActivity,使其重新获取对应焦点。这时,生命周期从onStop,执行完onNewIntent后,重新回到onResume。

//再次刷卡把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}


5.时间点达到16:52,再次刷卡,更新界面信息。此时,与第2步一致。

//待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}

6.按下back键,回到launcher,失去焦点。

//按下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().

7.按下Menu键清空后台任务,Activity被destroy。

//按下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



你可能感兴趣的:(Android,Android)