【发布时间】:2017-07-12 10:50:43
【问题描述】:
我从收到的notification 启动activity。如果按下notification,它将启动activity。如果back-stack 上没有以前的activities,或者只有某个,我想删除那个特定的activity 并在其中插入我的主要activity,而不是新活动。
我找到了这个Thread,但我不明白他是如何用两个意图和标志来处理它的。
即intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK)
按照他的方式做是明智的,还是我应该为此编辑活动堆栈?
我对 android dev 还很陌生,所以一些建议可以帮助我。 非常感谢 ;)
更新:所以我使用了 stackbuilder 但不知何故它没有设置正确......我没有找到我的错误,我的布尔 noActivity 肯定会设置,但我认为我误解了堆栈实际上是如何放置的以前的活动。
private void sendNotification(messageType type, Map<String, String> data, boolean noActivities) {
Intent i;
String message = "";
switch (type) {
case newFOLLOWER:
User cur = new User(data.get("other.name"));
User.lookAT = User.getOtherUserByName(cur);
i = new Intent(this, other_profile.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
message = data.get("other.name") + " is following you now. Click to see his profile";
i.putExtra("Notification", data.get("other.name") + " is following you now. Click to see his profile");
break;
default:
i = null;
break;
}
if (i != null) {
TaskStackBuilder stack = TaskStackBuilder.create(this);
if(noActivities){
stack.addParentStack(Photostream.class);
}
stack.addNextIntent(i);
PendingIntent pendingIntent = stack.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);//PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("PIC LOC")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
更新 2:所以经过大量搜索后,我发现我误解了堆栈构建器的工作原理。我找到了另一个线程,他们在其中描述了添加的工作原理。 Editing the Manifest in order to have a previous stack。 我要快速跳过你提供给我的部分教程......
感谢你们的帮助;)
【问题讨论】:
标签: android android-intent android-notifications