简体   繁体   中英

The click event of notification bar

When I click the notification bar, it skips the MainActivity.class interface:

Intent notificationIntent = new Intent(context,MainActivity.class); //After click the notification, it will skip to the activity 
PendingIntent contentIntent = PendingIntent.getActivity(context,0,notificationIntent,0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

If MainActivity.class itself has opened already, I click the notification bar again, it shows two interfaces. Who knows how to solve this?

Change your line:

Intent notificationIntent = new Intent(context,MainActivity.class);

to:

Intent notificationIntent = 
    new Intent(context,MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

as from the Android documentation it says:


If set, the activity will not be launched if it is already running at the top of the history stack.

or, if this is not the intended behavior check this flag:


If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

The following document could be of interest to you, when trying to understand what these flags will cause:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM