Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TaskStackBuilder in PendingIntent not working

I am trying to recreate a back stack for the Activity on notification pressed 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 it doesn't work. When I press the notification the Second activity is opened but pressing the back button, the activity is finished while I want to go back to First activity.

Setting parentActivityName in the AndroidManifest doesn't work.

What is wrong?

Thanks

like image 868
Matroska Avatar asked Dec 25 '22 16:12

Matroska


1 Answers

Try changing

PendingIntent.FLAG_UPDATE_CURRENT

to

PendingIntent.FLAG_CANCEL_CURRENT
like image 125
Ganesh Mohan Avatar answered Jan 05 '23 01:01

Ganesh Mohan