DaringLi
DaringLi

Reputation: 409

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?

Upvotes: 0

Views: 1988

Answers (1)

Briareos386
Briareos386

Reputation: 1957

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:

FLAG_ACTIVITY_SINGLE_TOP
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:

FLAG_ACTIVITY_CLEAR_TOP
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:

Upvotes: 1

Related Questions