I have a question about duplicating Android Notification.

Asked 2 years ago, Updated 2 years ago, 62 views

I came across Android Notification, but it didn't work as I wanted, so I'm asking you a question.

There are activities A and B, and A defined the notification and gave a notification.

I wrote the code together as below.

 NotificationCompat.Builder=
                        new NotificationCompat.Builder(MainActivity.this)
                                .setSmallIcon(R.drawable.ic_launcher_background)
                                .setContentTitle("My notification")
                                .setContentText("Hello World!");
                Intent resultIntent = new Intent(MainActivity.this, resultActivity.class);
                resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | | Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | | Intent.FLAG_ACTIVITY_SINGLE_TOP);

                TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
                stackBuilder.addParentStack(resultActivity.class);
                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent =
                        stackBuilder.getPendingIntent(
                                0,
                                PendingIntent.FLAG_UPDATE_CURRENT
                        );
                mBuilder.setContentIntent(resultPendingIntent);
                NotificationManager mNotificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(0, mBuilder.build());

With activity A on, pressing Notification shows the motion of activity B on.

However, if you press Notification while B activity is on, you can see the motion of B activity turning on again. But I don't want to bring B activity back because it's the same activity if B activity is on. What can I do?

android notification

2022-09-22 19:27

2 Answers

I think you can set activity B from Manifest to Single Top. Or try to operate after excluding the other two flags except single_top among the flags you set in resultIntent. For more information, see

https://developer.android.com/guide/components/tasks-and-back-stack?hl=ko

Check out the developer documentation.


2022-09-22 19:27

I think I solved it with difficulty. It's a self-answer. From the source code above,

 TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
   stackBuilder.addParentStack(resultActivity.class);
   stackBuilder.addNextIntent(resultIntent);

I think this is the problem.

1. If you press Notification while B activity is on, B activity continues to rise.

So I changed it like the following code.

 PendingIntent resultPendingIntent=
   PendingIntent.getActivity(MainActivity.this, 0, resultIntent,        
   PendingIntent.FLAG_UPDATE_CURRENT);

If you change it like this, the phenomenon like the one described above will disappear.

However, if you press the back key, there is a phenomenon that you return to activity A.

When I used TaskStackBuilder, I'm not sure, but B activity became the root because of addParentStack, so it seems to be over when I press back.

However, in the changed code, the task stack was intact, so it had to be handled separately. They said we need to put flag in int, change launch mode in Manifest, or solve it with source code.

I tried inten flag, launch mode several times, but failed because I didn't know the details, so I solved it with the source code. (The source code was the easiest.))

If activity A is not NULL in activity B simply, finish() so that activity A does not appear even if you press back in activity B.

I hope this method helps others.

틀린 Please let me know if there is anything wrong.


2022-09-22 19:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.