Question

I'm building an Android app that handles incoming events.

When I receive 1 such event, if my app is in the foreground, I immediately generate an AlertDialog that requires the user's attention.

However, when the app is in the background, the user does not see this, even though the AlertDialog has already been generated. If I manually go back to my app, I see the AlertDialog and I'm able to click OK or cancel.

I tried generating a notification dialog that appears when my app is in the background. This works, and I put the PendingIntent as MainActivity. When I tap on the notification, it brings me back to my app. However, the AlertDialog isn't there anymore.

Comparing the logcats of tapping on the notification vs manually going back to my app, I notice several differences.

When I manually go back to my app, I see MainActivity is resumed immediately. However, when I tap on the notification, it first goes back to the constructor of the class that generated the notification, a few other classes/fragments, before finally resuming MainActivity.

Does anyone know why this might be the case?

Thanks.

Was it helpful?

Solution

The default behaviour of a foreground notification is to re-launch the activity. Try setting your intent flag for the PendingIntent to use this:

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

And that will then have the same behaviour as if you navigated to the activity through your app.

From docs:

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

OTHER TIPS

it seems that,you are is creating new instance of app when resuming from notification so,add this in manifest,it might help:

android:launchMode="singleInstance"

in other case it resumes hence dialog going to be persisted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top