Activity启动模式设置:
<activity android:name=".MainActivity" android:launchMode="standard" />
Activity的四种启动模式:
1. standard
模式启动模式,每次激活Activity时都会创建Activity,并放入任务栈中。
2. singleTop
如果在任务的栈顶正好存在该Activity的实例, 就重用该实例,否者就会创建新的实例并放入栈顶(即使栈中已经存在该Activity实例,只要不在栈顶,都会创建实例)。
3. singleTask
如果在栈中已经有该Activity的实例,就重用该实例(会调用实例的onNewIntent())。重用时,会让该实例回到栈顶,因此在它上面的实例将会被移除栈。如果栈中不存在该实例,将会创建新的实例放入栈中。
4. singleInstance
在一个新栈中创建该Activity实例,并让多个应用共享改栈中的该Activity实例。一旦改模式的Activity的实例存在于某个栈中,任何应用再激活改Activity时都会重用该栈中的实例,其效果相当于多个应用程序共享一个应用,不管谁激活该Activity都会进入同一个应用中。
新信息到来之后弹出窗口,窗口是用Activity实现的,如果不做特殊设定,不论新信息窗口是否关闭,还会再弹出新的窗口。
需要做特殊设定的地方有
(1) Activity标签中设定属性:
android:launchMode=singleTask
(2) 在Service通过Intent启动Activity时,设定flags:
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
(3) 在MessageListActivity中添加onNewIntent(Intent intent)方法:
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.e("txrjsms", "ShowSmsActivity.onNewIntent");
list = SmsDataManager.getUnreadMessages(mContext);
if(list.size() == 0) {
finish();
return;
}
mHander.sendMessage(mHander.obtainMessage(
TxrjConstant.WHAT_UPDATE_SMS_DIALOG, 0, 0));
}
查看ShowSmsActivity运行日志,总计收到四条信息,
第1次调用onCreate方法,第2、3、4次调用onNewIntent方法
07-21 01:57:40.317: E/txrjsms(11639): ShowSmsActivity.onCreate
07-21 01:57:40.658: E/txrjsms(11639): ShowSmsActivity.onStart
07-21 01:57:40.658: E/txrjsms(11639): ShowSmsActivity.onResume
07-21 01:57:41.348: E/txrjsms(11639): ShowSmsActivity.onPause
07-21 01:57:45.813: E/txrjsms(11639): ShowSmsActivity.onResume
07-21 01:58:02.098: E/txrjsms(11639): ShowSmsActivity.onPause
07-21 01:58:02.098: E/txrjsms(11639): ShowSmsActivity.onNewIntent
07-21 01:58:02.109: E/txrjsms(11639): ShowSmsActivity.onResume
07-21 01:58:02.819: E/txrjsms(11639): ShowSmsActivity.onPause
07-21 01:58:05.272: E/txrjsms(11639): ShowSmsActivity.onResume
07-21 01:58:38.414: E/txrjsms(11639): ShowSmsActivity.onPause
07-21 01:58:38.414: E/txrjsms(11639): ShowSmsActivity.onNewIntent
07-21 01:58:38.434: E/txrjsms(11639): ShowSmsActivity.onResume
07-21 01:58:39.095: E/txrjsms(11639): ShowSmsActivity.onPause
07-21 01:58:47.082: E/txrjsms(11639): ShowSmsActivity.onNewIntent
07-21 01:58:47.102: E/txrjsms(11639): ShowSmsActivity.onResume
07-21 01:58:47.102: E/txrjsms(11639): ShowSmsActivity.onPause
07-21 01:58:53.238: E/txrjsms(11639): ShowSmsActivity.onResume
07-21 01:58:54.820: E/txrjsms(11639): ShowSmsActivity.onPause
07-21 01:58:54.880: E/txrjsms(11639): ShowSmsActivity.onStop
07-21 01:58:54.880: E/txrjsms(11639): ShowSmsActivity.onDestroy
查看MessageListActivity运行日志,它是Notification中打开的
收到5条信息
07-21 02:14:47.239: E/txrjsms(12413): MessageListActivity.onCreate
07-21 02:14:47.710: E/txrjsms(12413): MessageListActivity.onStart
07-21 02:14:47.710: E/txrjsms(12413): MessageListActivity.onResume
07-21 02:15:03.635: E/txrjsms(12413): MessageListActivity.onPause
07-21 02:15:03.635: E/txrjsms(12413): MessageListActivity.onNewIntent
07-21 02:15:03.835: E/txrjsms(12413): MessageListActivity.onResume
07-21 02:15:07.899: E/txrjsms(12413): MessageListActivity.onPause
07-21 02:15:07.899: E/txrjsms(12413): MessageListActivity.onNewIntent
07-21 02:15:08.099: E/txrjsms(12413): MessageListActivity.onResume
07-21 02:15:11.883: E/txrjsms(12413): MessageListActivity.onPause
07-21 02:15:11.883: E/txrjsms(12413): MessageListActivity.onNewIntent
07-21 02:15:12.063: E/txrjsms(12413): MessageListActivity.onResume
07-21 02:15:14.676: E/txrjsms(12413): MessageListActivity.onPause
07-21 02:15:14.676: E/txrjsms(12413): MessageListActivity.onNewIntent
07-21 02:15:14.886: E/txrjsms(12413): MessageListActivity.onResume
07-21 02:15:16.548: E/txrjsms(12413): MessageListActivity.onPause
07-21 02:15:17.068: E/txrjsms(12413): MessageListActivity.onStop
07-21 02:15:17.078: E/txrjsms(12413): MessageListActivity.onDestroy