在 onMessageReceived 调用的 activity 处按回 btn 后,FireBase-Prevent 重新启动应用程序
FireBase-Prevent relaunching app after pressing back btn at the activity called by onMessageReceived
我从 MainAcitivty 调用
startActivityForResult(new Intent(this, ActivityB.class), Constant.something);
我的应用程序在 ActivityB 的前台,我的设备收到了 firebase 的通知。 onMessageReceived 被调用
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getNotification() != null) {
openNewActivity(remoteMessage.getNotification().getBody());
}
}
private void openNewActivity(String messageBody) {
Intent resultIntent = new Intent(this, ActivityC.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle bundle = new Bundle();
bundle.putString(NotificationViewActivity.MESSAGE_EXTRA, messageBody);
resultIntent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , resultIntent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("TESTTTTT")
.setContentText(messageBody)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager ) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1000, builder.build());
}
当我的应用程序在前台时单击 firebase 的通知时,会出现 activityC。但是,当我在我的设备上 按下后退按钮 时,它 returns 到 MainActivity 而不是返回 ActivtyB 。因此,应用程序调用 MainActivity 的 onCreate,似乎重新启动了我的应用程序。有什么方法可以防止它,我想在按下后退按钮时,应用程序 returns MainActivty(希望它调用 onStart-> onResume 而不是 onCreate->onStart->onResume 的 MainActivity)
但是,当我使用简单的活动创建新项目时。它发生了我想要的但在我的项目中我是不对的。对不起我的语言
抱歉,上面的代码是正确的。我的队友在 activityC 的 onBackPressed 处放错了代码。该代码调用 MainActivity 并将意图的标志设置为 android.intent.action.MAIN 和 android.intent.category.LAUNCHER。它使应用 运行 像我上面描述的那样出错
我从 MainAcitivty 调用
startActivityForResult(new Intent(this, ActivityB.class), Constant.something);
我的应用程序在 ActivityB 的前台,我的设备收到了 firebase 的通知。 onMessageReceived 被调用
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getNotification() != null) {
openNewActivity(remoteMessage.getNotification().getBody());
}
}
private void openNewActivity(String messageBody) {
Intent resultIntent = new Intent(this, ActivityC.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle bundle = new Bundle();
bundle.putString(NotificationViewActivity.MESSAGE_EXTRA, messageBody);
resultIntent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , resultIntent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("TESTTTTT")
.setContentText(messageBody)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager ) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1000, builder.build());
}
当我的应用程序在前台时单击 firebase 的通知时,会出现 activityC。但是,当我在我的设备上 按下后退按钮 时,它 returns 到 MainActivity 而不是返回 ActivtyB 。因此,应用程序调用 MainActivity 的 onCreate,似乎重新启动了我的应用程序。有什么方法可以防止它,我想在按下后退按钮时,应用程序 returns MainActivty(希望它调用 onStart-> onResume 而不是 onCreate->onStart->onResume 的 MainActivity)
但是,当我使用简单的活动创建新项目时。它发生了我想要的但在我的项目中我是不对的。对不起我的语言
抱歉,上面的代码是正确的。我的队友在 activityC 的 onBackPressed 处放错了代码。该代码调用 MainActivity 并将意图的标志设置为 android.intent.action.MAIN 和 android.intent.category.LAUNCHER。它使应用 运行 像我上面描述的那样出错