15

I have an application that runs in the background and displays an error message via the notifications system. This notification has a pendingIntent that leads back to the app's main screen. On this main screen, I have set launchmode="singleTask". As I understand it from the Android Dev Guide, this should mean that my main activity will only ever have one instance.

However, if the user is viewing that activity at the time (or another one within the app), and goes and touches the notification to clear it, it goes ahead and puts another copy of the activity on the stack, so if I hit the back button, it will return to the main screen again (from the main screen).

Why would it be doing this?

Ramesh R
  • 7,009
  • 4
  • 25
  • 38
Ogre
  • 781
  • 3
  • 10
  • 30

3 Answers3

20

You are almost answering your own question in the question ;)

Try using:

android:launchMode="singleInstance"

Be warned though, if you are doing anythign like startActivityForResult - you will never receive the result!

Update:

If you want to receive new intent data using onNewIntent:

public void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);
    setIntent(intent);
}

That will change the intent the application returns when you use getIntent() to the new intent which was passed to onNewIntent.

Scoobler
  • 9,696
  • 4
  • 36
  • 51
  • 4
    Well, I went ahead and tried singleInstance instead. Unfortunately, it's still behaving in the same way. – Ogre Nov 16 '10 at 00:49
  • After having another look at the documentation, I'm wondering if I need to override onNewIntent() in my main activity, but I wouldn't know what to make it do. – Ogre Nov 16 '10 at 00:56
  • Ogre - see the update for info on using onNewIntent() and take a look at [Android “single top” launch mode and onNewIntent method](http://stackoverflow.com/questions/1711785/android-single-top-launch-mode-and-onnewintent-method) – Scoobler Nov 16 '10 at 09:08
  • 1
    Ok, so I did some testing after reading that. Turns out that it's never calling onNewIntent. So the only conclusion I can come to is that it simply does not do what the documentation says it does. – Ogre Nov 18 '10 at 00:31
  • One thing I did in this case was create a separate function to handle the bundle extras extraction from getIntent(). It was actually fairly esy. Basically I took all of that logic from onCreate() and pass in the Intent. This function now can be called from either onCreate() or onNewIntent() – Joe Plante Oct 09 '12 at 19:39
  • This is deeply weird, but it doesn't work. For android R you can't to avoid the creation of multi instances of one activity. When you will open Profiler, you will watch N instances and singleInstance and singleTask doesn't help to resolve it. If you know way how to resolve it, please ping me. – Georgiy Chebotarev Jun 09 '20 at 20:24
3

Check the docs of activity.launchmode.

I had a similar problem and solved it with android:launchMode="singleInstance".

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
2

It sounds to me like you want the following behaviour if the user selects the notification:

  • If the app is already running in a task, just bring that task to the foreground and show whatever activity happens to be on top of the activity stack.

  • If the app isn't already running in a task, launch the main activity of the app in a new task.

If this is the behaviour you are after, you don't need (or want) any special launch mode. See my answer to this question: Notification to restore a task rather than a specific activity?

This should do what you want.

Community
  • 1
  • 1
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • how can i return to the app (from service) when it's in the background ? – yanivtwin Sep 26 '18 at 11:12
  • 1
    @yanivtwin `Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName()); startActivity(intent);` This will bring the task from the background to the front, or start the app new if it isn't already running. – David Wasser Sep 26 '18 at 11:45