I know there's been a few posts for what I'm about to ask but I can't find any with the right answer.

From my understanding, if your main activity's (let's call it A) launchMode is set to singleTask, and A has initiated activity B then a click to the home button will destroy the history stack and re-launching the application will take you back to A and not B.

I have launchMode set to singleTask because I have a persistent notification and I don't want to have multiple instances of the main activity to appear whenever the user clicks on the notification.

Is there something I'm missing that would allow me to cater for both?

So I'm asking if there's a way I can ensure that whenever the user wishes to launch the app, from the notification or not, to take him back to the last (current) activity.

If I change launchMode to singleTop it works but I get multiple instances of the main activity whenever I launch it.

Thanks

Andreas

有帮助吗?

解决方案

Have you tried setting launchMode to singleTop to all the activities in your app?? Because what i get from your query is that the main activity isn't singleTop, so that might lead to another instance of the main activity being called once the main activity is launched from the activity that was launched from the notification activity.

Or you can specify the launchMode as an attribute to the application tag itself in the manifest.

其他提示

I use the following code to avoid multiple instances of the 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);

Changing manifest doesn't look appropriate to me

I'm having issues with both the approches. The notification works flawless only in this condition: - using the back button in the main activity (with the history containing only the that activity) - not using the Home button - not using the notification IF the activity you are calling is on top and active

In any other case, the notification cannot anymore call on the foreground the activity by using "new Intent(...)"

I've found the alchemical combination of manifest options and intent's flags for getting what I needed:

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 the element.

Now I've a notification which spawns the main activity (if that activity is not already in the foreground) and its behavior is correct even if the activity is "closed" by pressing the home button and/or the back one.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top