Pendingint, notification.

Asked 2 years ago, Updated 2 years ago, 21 views

    ServiceHandler handler = new ServiceHandler();
    manager = (NotificationManager)getSystemService(Service.NOTIFICATION_SERVICE);
    intent1 = new Intent(this, MainActivity.class);
    pendingIntent = PendingIntent.getActivity(this, 0, intent1,         PendingIntent.FLAG_UPDATE_CURRENT);
    NotiThread thread = new NotiThread(handler, this);
    thread.start();

The source is as above. What I'm curious about is that if I click the notification while the app is running, it will be executed again by putting it on top of the running app, so how can I prevent it from happening? If the app is running, I just want to keep it running.

android

2022-09-21 16:29

1 Answers

If you do not set launchMode for the activity, it will act as "standard" (default). This value means that you create a new activity each time you startActivity() new activity. LaunchMode can be set up in four ways: standard, singleTop, singleTask, and singleInstance, respectively. If you do not want to create a new activity with MainActivity on the screen (if it is at the top of the activity stack), try setting this value to singleTop. In this case, the onCreate() function is not called because no new activity is created, but the onNewIntent() of the existing activity is called.

Of course, singleTask, single Instance can have the same effect. But there's a slight difference. I think it would be helpful to read the official document and test it yourself for information.

Alternatively, if you do not want to modify launchMode in AndroidManifest.xml, and only want to do so when you click the notification, you can also give the FLAG_ACTIVITY_SINGLE_TOP flag when you create an entry for MainActivity in the code above. In this case, the "singletop" in louchMode will behave the same way.


2022-09-21 16:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.