50

I am facing exactly the problem mentioned in these links:

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

http://groups.google.com/group/android-developers/browse_thread/thread/77aedf6c7daea2ae/da073056831fd8f3?#da073056831fd8f3

http://groups.google.com/group/android-developers/browse_thread/thread/2d88391190be3303?tvc=2

I have a simple root activity with the LAUNCHER and MAIN intents and nothing else. I start another activity with has no flags or anything extra in the manifest whatsoever.

I launch the app (root activity) and from there start the 2nd activity. On pressing the Home button the task goes to the background. On launching the app again (from Launcher or from holding the Home button for recent apps) it starts a new instance of the root activity on top of the existing stack.

If I press the back button, the new "root" activity closes and the old 2nd activity is visible, which means its launching the root activity in the same task instead of bring the task to the foreground.

To counter this I made the root activity's launch Mode singleTask. Now when I press home and launch the app again, it clears the activities above the old root task and brings the old root task to the foreground instead of just bringing the entire old task with the 2nd activity on top to the front. Note that the old root task still retains its application state, which means it wasn't a new instance, but the the higher activities had been killed.

It even occurs on other applications downloaded from the market. The manual install method has no effect for me, it still launches the same way.

Monstieur
  • 7,992
  • 10
  • 51
  • 77
  • Kurian, I've faced the same issue. Is there any workaroun? Thanks in advance. – Vit Khudenko Apr 19 '10 at 13:43
  • 1
    Just keep pressing back and exit the application completely after deploying it from Eclipse. Launch it again from the phone. – Monstieur May 05 '10 at 12:28
  • Possible duplicate of [Activity stack ordering problem when launching application from Android app installer and from Home screen](http://stackoverflow.com/questions/6356467/activity-stack-ordering-problem-when-launching-application-from-android-app-inst) – blahdiblah May 19 '14 at 21:42

4 Answers4

25

This is due to the intents being used to start the app being different. Eclipse starts an app using an intent with no action and no category. The Launcher starts an app using an intent with android.intent.action.MAIN action and android.intent.category.LAUNCHER category. The installer starts an app with the android.intent.action.MAIN action and no category.

Whoever submitted the bug should have probably worded it as a request for enhancement to the Eclipse plugin since they apparently want Eclipse to have the ability to pretend to be the launcher and to start apps using the same intent as the launcher.

Lance Nanek
  • 6,377
  • 2
  • 24
  • 19
  • 2
    The problem however is this persists even when launching the application from the phone directly... – Monstieur Feb 17 '10 at 19:28
  • 1
    Yes, Kurian is right - this is not just an Eclipse plugin issue. The same happens on device on OTA upgrade. This is the most weird Android issue I've faced with. Can't believe it's still a New issue being created 1 year ago!!! – Vit Khudenko Apr 19 '10 at 13:10
  • 1
    Doesn't look like this has been resolved to date. Hmmm... wonder what the folks over there really do? – source.rar Aug 02 '11 at 18:03
  • Hmm, my tests show that Lance is correct. I get that my startupActivity.onCreate is executed every time I resume *if I run it from Eclipse*. But if I start from the device, I do not get the onCreate executed when resuming... Running 2.3.3 on Samsung S2. – Ted Oct 13 '11 at 09:12
  • However, the second Activity (that is launched) won't show up on resume, then the damn StartupActivity is shown again... hmm – Ted Oct 13 '11 at 09:22
  • What is this? How can it not be fixed? – JohnyTex Oct 14 '14 at 09:59
  • @Lance Nanek and all members I am facing the same issue is there any workaround for that? – Hardik Mehta Sep 11 '17 at 13:07
6

Here is the solution:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0 & getIntent().getExtras() == null) {
        finish();
        return;
    }

 Your code....
}

EDIT: I had problems with new intents and notifications. The previous solution doesn't work with notifications and intents...

toni
  • 1,674
  • 1
  • 15
  • 15
2

Just add this in onCreate method of your launcher activity like this:

      @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    if (!isTaskRoot()) {
        finish();
        return;
    }
    // other function
    }
Amal Kronz
  • 1,657
  • 1
  • 18
  • 22
0

Similar solution for Xamarin.Android:

if (!IsTaskRoot)
            {
                string action = this.Intent.Action;
                if (this.Intent.HasCategory(Intent.CategoryLauncher) && !string.IsNullOrEmpty(this.Intent.Action) && action == Intent.ActionMain)
                {
                    Finish();
                    return;
                }
            }
CDrosos
  • 2,418
  • 4
  • 26
  • 49