Notification.Builder builder = new Notification.Builder(getContext());
builder.setAutoCancel(true);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.ic_launcher));
builder.setContentTitle("abc");
builder.setContentText("abc");
builder.setContentIntent(PendingIntent.getActivity(getContext(), 0,
new Intent(getContext(), MainActivity.class).replaceExtras(bundle).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP),
PendingIntent.FLAG_UPDATE_CURRENT));
builder.setDefaults(Notification.DEFAULT_SOUND);
NotificationManager manager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
设置启动模式的第一种方法。
<activity
android:name=".activity.mainActivity.MainActivity"
android:theme="@style/AppTheme.NoActionBar"
android:launchMode="singleTask"/>
设置启动模式的第二种方式。
Google官方文档。
FLAG_ACTIVITY_NEW_TASK
在新任务中启动Activity
。如果任务已在为您正在启动的活动运行,则该任务将恢复到前台,并恢复其上一个状态,并且活动将在onNewIntent()
中接收新的意图。
这会产生与“singleTask”launchMode值相同的行为,如上一节所述。
活动堆栈a,b,MainActivity
为什么onNewIntent()
没有以第一种方式调用。
为什么活动(a,b)没有以第一种方式清理。
答案 0 :(得分:0)
使用android:launchMode="singleTask"
“ singleTask ”和“ singleInstance ”活动只能开始执行任务。它们始终位于活动堆栈的根部。此外,设备一次只能保存一个活动实例 - 只有一个这样的任务。
使用FLAG_ACTIVITY_NEW_TASK
设置,此活动将成为此历史堆栈上新任务的开始。