Starting Activity Through Notification: Avoiding Duplicate Activities

Starting Activity through notification: Avoiding duplicate activities

That's the way it's supposed to be by default. You probably need to specify android:launchMode="singleTop" if you want to have a single instance only.

There are 4 launch modes, more info here: https://developer.android.com/guide/topics/manifest/activity-element.html

Launch activity from notification instantiating activity even if already there

First of all, try to set PendingIntent flag to PendingIntent.FLAG_UPDATE_CURRENT instead of 0:

PendingIntent launchActivity = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

Then set android:launchMode="singleTop" for the Activity A (i.e. MainActivity) to avoid recreating it when it's opened:

<activity
android:name=".MainActivity"
...
android:launchMode="singleTop">
...
</activity>

Finally in the MainActivity, do the Intent handling in onCreate (when the Activity was destroyed and now created by clicking on notification) and onNewIntent method (when the Activity is created and we want to handle notification click).

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}

private void handleIntent(Intent intent) {
if(intent.hasExtra("KEY_TO_EXTRA")){
// ...
}
}

Android Prevent Notification if in desired Activity

CommonsWare provides a solution to this as an answer to this question.

Specifically, he links to this blog post, which explains the use of ordered broadcasts.

As a not-so-efficient idea, you could implement a broadcast receiver in your activity and service, then before sending the notification, send a broadcast intent to your activity and wait for a response. If you don't get a response, raise the notification.

Duplicate MainActivity When Enter From Notification

Yes It is.. You have to add this flag to your pending Intent.

Intent intent = new Intent(_context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent activity = PendingIntent.getActivity(_context, 0, intent, 0);

Taken form here,

https://stackoverflow.com/a/7308940/603744

Duplicate app when click in notification bar

When you are creating intent use this flag

Intent m_intent = new Intent(this, YourActivity.class);
m_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

and then use this intent in pending intent

Notification click: activity already open

You need to set the launchMode attribute of the Activity you are starting to singleTop. This will cause incoming Intents to be delivered to the existing instance rather than starting a new instance when that Activity is already at the top of the task's stack.

This is done in the manifest by adding android:launchMode="singleTop" to the <activity> element. To access the latest Intent (if you are interested in any data that may have passed in with it), override onNewIntent() in your Activity.

How to avoid multiple instances of same Activity?

Setting either the following flags may help you to resolve your issue:

  • Intent.FLAG_ACTIVITY_CLEAR_TOP
  • Intent.FLAG_ACTIVITY_REORDER_TO_FRONT


Related Topics



Leave a reply



Submit