Take the 2-minute tour ×

I've come across a bug in my application when it is launched using the "Open" button on the android market. It seems that lauching it from the market uses a different intent then launching it from the phone's applications menu. This is leading to multiple copies of the same activity being launched, which are conflicting with each other.

For example, if my app consists of the activities A-B-C then the above issue can lead to a stack A-B-C-A.

I tried using android:launchMode="singleTask" on all the activities to fix this problem, but it has the unwanted side-effect of clearing the activiy stack to root whenever I hit HOME. Example: A-B-C -> HOME -> A when what I need is A-B-C -> HOME -> A-B-C

Is there a good way to prevent launching multiple activities of the same type without reseting to the root activity when using HOME?

share|improve this question
add comment

6 Answers

Add this to onCreate and you should be good to go:

// Possible work around for market launches. See http://code.google.com/p/android/issues/detail?id=2373
// for more details. Essentially, the market launches the main activity on top of other activities.
// we never want this to happen. Instead, we check if we are the root and if not, we finish.
if (!isTaskRoot()) {
    final Intent intent = getIntent();
    final String intentAction = intent.getAction(); 
    if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
        Log.w(LOG_TAG, "Main Activity is not the root.  Finishing Main Activity instead of launching.");
share|improve this answer
I've been trying to solve this bug for years, and this was the solution that worked, so thank you very very much! I need to also note that this isn't just a problem within the Android Market, but also sideloading an app by uploading it to a server, or emailing it to your phone, causes this problem. All these things install the app using the Package Installer, where I believe the bug resides. Furthermore, just in case it's not clear, you only need to add this code to the onCreate method of what your root activity is. –  ubzack Dec 14 '11 at 21:28
I find it very odd that this happens in a signed app deployed to the device, but not a debug version deployed from Eclipse. Makes it quite difficult to debug! –  Matt Connolly Jan 20 '12 at 6:11
This does happen with a debug version deployed from Eclipse as long as you START it also via Eclipse (or IntelliJ or other IDE). It has nothing to do with how the app gets installed on the device. The problem is due to the way the app is started. –  David Wasser May 30 '12 at 8:31
Does anyone know if the same problem exists for Google Play? –  Dave Cameron Jul 18 '12 at 16:09
Does anyone know if this code will ensure that the existing instance of the app will be brought to the foreground? Or does it just call finish(); and leave the user with no visual indication that anything has happened? –  Carlos P Nov 4 '12 at 15:47
show 5 more comments

Have you tried the singleTop launch mode?

Here is some of the description from http://developer.android.com/guide/topics/manifest/activity-element.html:

... a new instance of a "singleTop" activity may also be created to handle a new intent. However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created. In other circumstances — for example, if an existing instance of the "singleTop" activity is in the target task, but not at the top of the stack, or if it's at the top of a stack, but not in the target task — a new instance would be created and pushed on the stack.

share|improve this answer
I thought of that, but what if the activity is not at the top of the stack? For example, it seems like singleTop will prevent A-A, but not A-B-A. –  bsberkeley Dec 3 '10 at 1:23
Can you achieve what you want by using singleTop and the finish methods within Activity? –  elevine Dec 3 '10 at 1:37
I don't know if it will quite accomplish what I want. Example: If I'm on activity C after popping A and B, then a new activity A gets launched and I'll have something like C-A won't I? –  bsberkeley Dec 3 '10 at 6:02
Its hard to answer this without understanding more about what these activities do. Can you provide more details about your application and the activities? I wonder if there is a mismatch between what the Home button does and how your want it to act. The home button does not exit an Activity, it "backgrounds" it so the user can switch to something else. The back button is what exits/finishes and activity. Breaking that paradigm might confuse/frustrate users. –  elevine Dec 3 '10 at 17:53
I've added another answer to this thread so you can see a copy of the manifest. –  bsberkeley Dec 3 '10 at 23:35
add comment

Perhaps it is this issue? Or some other form of the same bug?

share|improve this answer
See also code.google.com/p/android/issues/detail?id=26658, which demonstrates that it is caused by things other than Eclipse. –  Kristopher Johnson Aug 19 '13 at 16:21
add comment

I had the same problem, and I fixed it using the following solution.

In your main activity add this code on the top of the onCreate method:

ActivityManager manager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<RunningTaskInfo> tasks =  manager.getRunningTasks(Integer.MAX_VALUE);

for (RunningTaskInfo taskInfo : tasks) {
    if(taskInfo.baseActivity.getClassName().equals(<your package name>.<your class name>) && (taskInfo.numActivities > 1)){

don't forget to add this permission in your manifest.

< uses-permission android:name="android.permission.GET_TASKS" />

hope it helps you.

share|improve this answer
add comment

I had this problem also

  1. Don't call finish(); in the home activity it would run endlessly - home activity is being called by ActivityManager when it finished.
  2. Usually when the configuration is changing (i.e. rotate screen, change language, telephony service changes i.e. mcc mnc etc.) the activity recreate - and if the home activity is running then it calls again to A. for that need to add to manifest android:configChanges="mcc|mnc" - if you have connection to cellular, see http://developer.android.com/guide/topics/manifest/activity-element.html#config for which configuration there is when booting the system or push open or whatever.
share|improve this answer
add comment

try using SingleInstance launch mode with affinity set to allowtaskreparenting This will always create the activity in new task but also allow its reparenting. Check dis :Affinity attribute

share|improve this answer
Probably won't work because, according to the documentation "re-parenting is limited to the "standard" and "singleTop" modes." because "activities with "singleTask" or "singleInstance" launch modes can only be at the root of a task" –  bsberkeley Dec 3 '10 at 5:51
add comment

Your Answer


