LaunchMode = singleTask and notifications? Android

I know that there were several messages for what I was going to ask, but I can not find the right answer.

From my point of view, if your main activity (let it be called A) launchMode is set to singleTask, and A initiates action B, then pressing the home button will destroy the history stack and restarting the application will bring you back to A, not B.

I have set the startup mode for singleTask because I have a constant notification, and I do not want each instance of the main activity to be displayed every time the user clicks on the notification.

Is there something that I am missing that will allow me to serve both?

So, I ask if there is a way to guarantee that whenever the user wants to start the application from the notification or not, return it to the last (current) action.

If I change startMode to singleTop, it works, but I get multiple instances of the main activity whenever I start it.

thanks

Andreas

+4
source share
4 answers

Have you tried setting launchMode to singleTop for all the actions in your application? Because what I get from your request is that the main action is not singleTop, so this may cause another instance of the main action to be called after the main action is launched from an activity launched from an activity notifications.

Or you can specify startMode as an attribute of the application tag itself in the manifest.

+1
source

I use the following code to avoid multiple instances of activity

Intent intent=new Intent(this,RICO.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); 

Manifest change is not right for me

+2
source

I have problems with both settings. The notification works flawlessly only in this state: - using the "Back" button in the main action (with a history containing only this activity) - not using the "Home" button - do not use the notification if the action you call is on top and is active

In any other case, the notification can no longer trigger actions in the foreground using "new Intent (...)"

0
source

I found an alchemical combination of manifest options and intent flags to get what I need:

 Intent intent= new Intent(this, YaampActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 

using these options

 android:launchMode="singleTask" android:taskAffinity="" android:excludeFromRecents="true" 

inside an element.

Now I have a notification that generates the main action (if this activity is not yet in the foreground), and its behavior is correct, even if the activity is "closed" by pressing the "home" button and / or vice versa.

0
source

Source: https://habr.com/ru/post/1344481/


All Articles