2

According to Android docs:

http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html

"When the user leaves a task by pressing the Home button, the current activity is stopped and its task goes into the background. The system retains the state of every activity in the task. If the user later resumes the task by selecting the launcher icon that began the task, the task comes to the foreground and resumes the activity at the top of the stack."

If I understand this correctly, this means:

  1. Activity A as MAIN Activity.
  2. Activity B that gets started in A, through "startActivity" - common, plain intent instance.
  3. I open app for first time, A gets loaded.
  4. I click on button in A and B is openend.
  5. I press home button.
  6. I open app again, for 2nd time, and B is expected to be shown

...right? I suppose this is the correct behavior to expect...

However, I am not seeing this in my app.

If I hit "Home button" and then resume my app, by pressing the launcher icon, it will start with the main activity - not the one at the top or latest one.

I am coding on a Samsung Galaxy Tab Android 2.2.1 - I have the most common options in the Android manifest - thing is that I handle like 10 different activities with different intent extras - and a Dispatcher class approach - or save each activity state - sounds quite demanding.

I am using Eclipse IDE with ADT version 12; and I found something very interesting:

When I run the app from the Eclipse IDE, with my device connected, I don't see this behavior. The app behaves as stated in the docs. In fact, I saw this only after I deployed my apk at the Google Play app repository; and downloaded it to test.

My question is, has anybody found the real reason why is this happening? Is the documentation wrong? or missing something? Is this a bug on Android?

Another research I have done is:

When I try my app, downloaded from the google play, as APK, if I enter my app for the 2nd time, I get the "main" activity instead of the last one openend. I press home. After pressing home, I enter application management settings for android, locate my app and click on "force stop". After doing this, the app behaves as stated in the docs.

Somebody help! :)

Valdez V.
  • 252
  • 5
  • 18

3 Answers3

9

This is a bug in android's platform:

http://code.google.com/p/android/issues/detail?id=2373

The workaround is, to place this in the onCreate method of your main Activity:

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.");
        finish();
        return;       
    }
}

as extracted from:

How to prevent multiple instances of an activity when it is launched with different intents

...spent 3 days looking out for this.

Community
  • 1
  • 1
Valdez V.
  • 252
  • 5
  • 18
  • Just make sure your manifest is as "plain" as possible - do not add any launch modifiers...on your activity tag. – Valdez V. Jun 14 '12 at 22:38
  • 1
    perfect answer , solved our 3 day problem with ease. thank you! – iremk Mar 28 '13 at 08:51
  • 1
    damn, I was trying to figure out what is going on for last two days and probably I would spend couple more if I would not found that answer.. thanks a lot mate! Looks like it also apply when you're redirecting user to somewhere outside the app, for example gps settings and user is returning to app using back button.. I was wondering why activity was recreated but state bundle was not provided – lupatus Jun 18 '13 at 22:09
2

I'm just going to explain why it fails, and how to reproduce this bug programmatically so you can incorporate this in your test suite:

  1. When you launch an app through Eclipse or Market App, it launches with intent flags: FLAG_ACTIVITY_NEW_TASK.

  2. When launching through the launcher (home), it uses flags: FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_BROUGHT_TO_FRONT | FLAG_ACTIVITY_RESET_TASK_IF_NEEDED, and uses action "MAIN" and category "LAUNCHER".

If you would like to reproduce this in a test case, use these steps:

adb shell am start -f 0x10000000 -n com.testfairy.tests.regression.taskroot/.MainActivity 

Then do whatever is needed to get to the other activity. For my purposes, I just placed a button that starts another activity. Then, go back to the launcher (home) with:

adb shell am start -W -c android.intent.category.HOME -a android.intent.action.MAIN

And simulate launching it via the launcher with this:

adb shell am start -a "android.intent.action.MAIN" -c "android.intent.category.LAUNCHER" -f 0x10600000 -n com.testfairy.tests.regression.taskroot/.MainActivity

If you haven't incorporated the isTaskRoot() workaround, this will reproduce the problem. We use this in our automatic testing to make sure this bug never occurs again.

Hope this helps!

gilm
  • 7,690
  • 3
  • 41
  • 41
1

The docs are right, the only possible problem I can think of that is causing this is the device you are testing on, if it works as expected on the emulator (which is stock Android) it should work on at least 90% of Androids, its the manufactures fault for this I believe not Android.

FabianCook
  • 20,269
  • 16
  • 67
  • 115
  • Thank you for your response. What I *suspect* is that I might get the same behavior I am seeing when I install the app through eclipse. Perhaps I should try uploading the APK through the emulator commands and see what happens... – Valdez V. Jun 13 '12 at 03:39
  • 1
    Yes, I have the expected behavior on the emulator...but why is it changing on the device...?? Also, did you read about the "force stop" action I mention? That's really ODD. So far I have tested on 2 HTC phones with 2.3.X, and 2.1 ...as well as one LG with 2.3.X...and I DON'T get the expected behavior...when I press home and go back to the app, it loads the MAIN activity instead of the latest one on top of the stack :S – Valdez V. Jun 13 '12 at 03:45
  • You have nothing in your activity that calls finish right? The application is just been removed from the application stack. – FabianCook Jun 13 '12 at 22:09
  • See my answer, I could not believe it but yes, it is a bug in android's platform... – Valdez V. Jun 14 '12 at 22:19
  • Looks like it has already been reported twice... It's really random and discouraging... I thought it had something to do with the device model or anything... Thanks for following this up SmartLemon :D – Valdez V. Jun 14 '12 at 22:21
  • Ahh okay, Should be fixed soon then – FabianCook Jun 14 '12 at 22:22
  • It's been there for more than 2 years XD I suppose they are lazy as there is a work-around. – Valdez V. Jun 14 '12 at 22:38
  • I see your answer is loosely based of mine? – FabianCook Jun 14 '12 at 22:40
  • Oh, well, your comment is related to the stack, but I found the answer I needed in that other post... :) – Valdez V. Jun 16 '12 at 01:07