title: Activity stack ordering problem when launching application from Android app installer and from Home screen
body: <p>For testing purposes only, I am allowing my app APK to be downloaded and installed via a URL. Once downloaded on the phone, it can be launched with the Android app installer which gives the user an option to install it to their device and then run it.</p>
  
  <p>Consider if we downloaded and ran the app in the way described above. The main/launcher activity in my app is a login page (<code>Activity A</code>). Once the user is authenticated, they are taken to the main area of the application, e.g. <code>Activity B</code>. So now the current activity stack of this task is <code>A &gt; B</code>.</p>
  
  <p>I then press the home button on the phone and am taken to the Android home screen. I re-launch my app via the icon in the menu, and I am taken to <code>Activity A</code>, instead of <code>Activity B</code>. Either the activity stack is now <code>A &gt; B &gt; A</code>, or there are now two separate tasks with activity stacks <code>A &gt; B</code>, and <code>A</code> respectively. What I want is to be taken back to <code>Activity B</code> when I relaunch the app. Pressing back whilst in this state will take me back to <code>Activity B</code>.</p>
  
  <p>This undesired behaviour only happens if I first open the app via the installer, and not if I open the app via the home screen/menu.</p>
  
  <p>I looked into how the activities are being started by each mechanism. When we use the app installer, we see the following logs:</p>
  
  <pre><code>INFO/ActivityManager(XXXX): Starting activity: Intent { dat=file:///mnt/sdcard/download/[my app].apk cmp=com.android.packageinstaller/.InstallAppProgress (has extras) }
  INFO/ActivityManager(XXXX): Starting activity: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=[my package]/[Activity A] }
  </code></pre>
  
  <p>via launcher / home screen:</p>
  
  <pre><code>INFO/ActivityManager(XXXX): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=[my package]/[Activity A] }
  </code></pre>
  
  <p>When started with the installer we see it is using the flag <code>0x10000000</code>, but when started with the launcher we see it is using <code>0x10200000</code>. It is also using an intent category.</p>
  
  <p>From the <a href="http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK" rel="noreferrer">docs</a> we see the flags are:</p>
  
  <pre><code>public static final int FLAG_ACTIVITY_NEW_TASK
  Constant Value: 268435456 (0x10000000)
  
  public static final int FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
  Constant Value: 2097152 (0x00200000)
  </code></pre>
  
  <p>The flag <code>FLAG_ACTIVITY_RESET_TASK_IF_NEEDED</code> (which is being used when the app is launched from the launcher) seems to usually prevent a new task from being created if one already exists, and will restore the last used activity. This is the desired behaviour. Why is it not working in this situation? Is there anything I can do to make sure my application will always return me to the last Activity regardless of whether it was started through the app installer/launcher?</p>
  
  <p>If I use <code>singleTask</code> it will always take me back to the main activity (<code>Activity A</code>) whenever I run the app (which is also not desirable).</p>
  
  <p>Here is a question I found where someone is experiencing a similar problem (which has no accepted answer): <a href="https://stackoverflow.com/questions/5318885/app-loses-its-ability-to-remember-its-stack-when-launched-from-another-applicatio">App loses its ability to remember its stack when launched from another application</a></p>
  
  <p>EDIT: Checking for the flag <code>FLAG_ACTIVITY_BROUGHT_TO_FRONT</code> in <code>onCreate()</code> of our launcher activity (and then finishing if it is set) seems to fix the main symptom, but clearly the underlying issue is still there. Is there a more complete fix?</p>
  
  <p>EDIT2: The same result occurs when you download/run the app from the Android Market, so some of the above details may not be relevant.</p>