【发布时间】:2020-04-06 22:33:42
【问题描述】:
我正在尝试从通知开始活动。启动该活动后,我通过 intent.putextra 将数据添加到意图,以便活动显示适合情况的正确内容。
正在启动的活动应该只在堆栈中打开一次。我确实通过
实现了这一点android:launchMode="singleTop"
在我的清单中。
但是 - 现在我来回答我的问题 - 如果此活动已经在运行,我希望它关闭并将其替换为我正在使用特定附加数据创建的实例(添加额外数据)。我怎样才能做到这一点?
这是我的通知代码:
public void newMessageNotification(String title, String message, String otherusernumber, String requestStatus, String sendername) {
notificationManager = NotificationManagerCompat.from(this);
Intent chatIntent = new Intent(this,ChatActivity.class);
//these three strings define the behaviour of the chat activtity
chatIntent.putExtra("otherUsername", sendername);
chatIntent.putExtra("otherUsernumber", otherusernumber);
chatIntent.putExtra("requestStatus", requestStatus);
chatIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), chatIntent,
PendingIntent.FLAG_UPDATE_CURRENT);;
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_new_message)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.build();
notificationManager.notify(1, notification);
}
【问题讨论】:
标签: android android-intent android-activity notifications