title: How to prevent app restart when using launcher after store?
body: <p>My Android application is being restarted when using the launcher to launch it after I have used the app store to launch it (and visa versa). Is there any way to prevent this?</p>
  
  <p>By restarted I mean that the activity stack is lost. This is important as our users are setting up and returning to an activity in the app intermittently over the course of an hour or so. After first install, they will likely have installed and opened the app from the app store, set themselves up and then backgrounded the app. Later they are likely to open the app from the launcher and lose all their state!</p>
  
  <p>The problem is further compounded as we start a foreground service along with the set-up activity. Clicking the services notification should bring the user back to the set-up activity but, as with the launcher, if the user originally opened the app from the Play store, they again lose all their state!</p>
  
  <p><strong>Reproducing the problem</strong></p>
  
  <p>I've made a sample application here:</p>
  
  <p><a href="https://github.com/samskiter/LaunchTest" rel="nofollow">https://github.com/samskiter/LaunchTest</a></p>
  
  <p>Note: it uses the BBC weather application package Id in order to allow you to quickly open from the app store (the "Open" button will be shown on the BBC weather application if this app is installed).</p>
  
  <p>Steps are as follows:</p>
  
  <ol>
  <li>Uninstall the BBC weather app if you have it</li>
  <li>Install the LaunchTest app</li>
  <li>Close the LaunchTest app from recents</li>
  <li>Open the LaunchTest app from the BBC weather app page on the Play Store</li>
  <li>Click the button to navigate to the <strong>Second</strong> activity</li>
  <li>Background the application (press home)</li>
  <li>Open the LaunchTest application from the app launcher</li>
  <li>The state is lost! you are back at the <strong>First</strong> (root) activity</li>
  </ol>
  
  <p><strong>What I've tried</strong></p>
  
  <p>Using <code>singleTask</code> launch mode hasn't helped - it causes the app to be relaunched even if you use the launcher every time.</p>
  
  <p>I've tried <code>alwaysRetainTaskState</code> - I don't really expect this to work as this only really affects things over about a 30 minute wait.</p>
  
  <p><strong>What I think is going on</strong></p>
  
  <p>There is no mechanism in the activity manager / intent system to open a running app in it's current state. Instead, I think the UID of the launching application is taken into account. If it is different then the <code>Intent.FLAG_ACTIVITY_NEW_TASK</code> flag is forced and so created a new task and dropping all my users' lovely state.</p>
  
  <p><strong>Inspecting Google Maps</strong></p>
  
  <p>Google maps has a very similar interaction model to our application: a setup UI, followed by an ongoing process the user is going through for a long period of time (navigation) with a paired, foreground service (the navigation service you can see in your notification bar). BUT GMaps doesn't suffer from this problem. I think this is because it uses only a single activity for all of it's interface <strong>and</strong> uses <code>singleTask</code>. So now, when you tap on the launcher after originally launching from the play store, the task can be reused.</p>
  
  <p>In my opinion this exposes a hole in the android intent/activity management system. The whole point of the <code>savedInstanceState</code>/activity lifecycle is to prevent dropping state, but here we have a way to dump everything. My current best solution is to detect app restarts by the fact the service is running and try to get the user back to where they were, which is more than a little tricky.</p>
  
  <p><strong>If someone knows a way I can prevent my state being dropped on the floor when reopening from the app launcher after opening from the store, I would really appreciate it.</strong></p>