Android stackBuilder.addParentStack不工作-导航到主活动(OnBackPressed)
当用户单击通知时,它将打开工作正常的新活动。但当我按下后退按钮时,它关闭了应用程序 我想要什么? 当我点击后退按钮时,它每次都返回MainActivity(所选活动)Android stackBuilder.addParentStack不工作-导航到主活动(OnBackPressed),android,android-notifications,Android,Android Notifications,当用户单击通知时,它将打开工作正常的新活动。但当我按下后退按钮时,它关闭了应用程序 我想要什么? 当我点击后退按钮时,它每次都返回MainActivity(所选活动) private void generateNotificationNew(Context context, String message,String pushIdmessage) { Intent resultIntent = null; NotificationCompat.Builder
private void generateNotificationNew(Context context, String message,String pushIdmessage) {
Intent resultIntent = null;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(context.getString(R.string.app_name))
.setAutoCancel(true)
.setContentText(message);
resultIntent = new Intent(context,ResultActivity.class);
//Intent resultIntent = new Intent(this, AvailableJobActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
5,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
}
stackBuilder.addParentStack
或stackBuilder.addnextint
可能无法正常工作。
任何其他选项谢谢。对于生成通知您需要输入以下代码
NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Notificatin Title")
.setContentText("message");
Intent notificationIntent = new Intent(this , ResultActivity);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0 , PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify( 0 , builder.build());
对于handleback事件,您需要在结果活动中的onBackPressed()中检查以下内容
@Override
public void onBackPressed() {
if(this.isTaskRoot())
startActivity(new Intent(this , MainActivity.class));
super.onBackPressed();
}
现在,您可以从通知启动的活动重定向到MainActivity。。
享受它…-)