TaskStackBuilder in PendingIntent not working

I am trying to restore the back stack for Activity on a notification clicked with this code:

Intent firstIntent = new Intent(this, First.class);  
Intent secondIntent = new Intent(this, Second.class);    
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

 stackBuilder.addNextIntent(firstIntent);
 stackBuilder.addNextIntent(secondIntent);
 PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
 Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

 NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                                    .setSmallIcon(R.mipmap.ic_launcher)
                                    .setContentTitle(fromName)
                                    .setContentText(message)
                                    .setAutoCancel(true)
                                    .setSound(defaultSoundUri)
                                    .setContentIntent(pendingIntent);

  NotificationManager notificationManager =
                                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

  notificationManager.notify(0 ,notificationBuilder.build());

but that will not work. When I click on the notification, the second activity opens, but by pressing the "Back" button, the action ends until I want to return to the first activity.

Setting parentActivityName in AndroidManifest does not work.

What's wrong?

thank

+4
source share
1 answer

Try to change

PendingIntent.FLAG_UPDATE_CURRENT

to

PendingIntent.FLAG_CANCEL_CURRENT
+5
source

Source: https://habr.com/ru/post/1598508/


All Articles