如果用户输入Geofence,我会使用GoogleTransitionIntentService显示通知,但通知会重新创建MainActivity.class。我想恢复这项活动。 https://github.com/googlesamples/android-play-location/tree/master/Geofencing
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent notificationPendingIntent =
PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setColor(Color.RED)
.setContentTitle(notificationDetails)
.setContentText("Test")
.setContentIntent(notificationPendingIntent);
builder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());
清单:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
答案 0 :(得分:1)
您必须在给定活动的清单中使用launchmode作为singleTop。
android:launchMode = "singleTop"
答案 1 :(得分:1)
尝试在清单中为MainActivity添加android:launchMode="singleTop"
。
答案 2 :(得分:0)
您可以添加
等标记notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
并设置
android:launchMode = "singleTop"
清单中的活动。
来自GoogleDoc
如果活动具有启动模式singleTop(或up intent包含FLAG_ACTIVITY_CLEAR_TOP),则将父项置于堆栈顶部,并保留其状态。活动的onNewIntent()方法接收意图。如果活动具有启动模式标准(并且向上意图不包含FLAG_ACTIVITY_CLEAR_TOP),则当前活动及其父项都将从堆栈中弹出,并创建父活动的新实例以接收导航意图。