title: Why is onStop being called right after my Activity is launched?
body: <p>I have an activity that needs to turn on the screen when it is started (just in case the screen was off). So in my <code>onCreate</code>, I have:</p>
  
  <pre><code>getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
              |WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
              |WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
              |WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
  </code></pre>
  
  <p>Using this straightforward combination, I am able to cause my activity to display whenever it is started from my background service (yes, this is a legitimate case).</p>
  
  <p>The problem, however, is that there is a very strange lifecycle behavior when I start up my activity in such a case. Using extensive logging, I was able to figure out that the following 7-step process happens <strong>immediately</strong> after the activity is started:</p>
  
  <ol>
  <li>onCreate</li>
  <li>onStart</li>
  <li>onResume</li>
  <li>onPause</li>
  <li>onStop</li>
  <li>onStart</li>
  <li>onResume</li>
  </ol>
  
  <p>See that? For a simple activity start, onStart is called twice. And more importantly, onStop is mysteriously called, even though the activity was just started - and nothing happened that would cause it to stop.</p>
  
  <p>I have tested this in many different scenarios and it appears that this strange behavior only happens when the screen is off and the activity is launched after it was destroyed. If the screen is on, or if the activity was stopped [but not yet destroyed] the activity launches normally, and onStart is only called once.</p>
  
  <p><strong>Bottom line</strong>: it appears that when my activity is launched and the screen is forced on, Android starts the activity, then stops it, and then starts it again for no apparent reason.</p>
  
  <p>So: why is this happening? And is there anything I can do to workaround this (so that onStop isn't called until there is a legitimate cause for it to be)?</p>
  
  <hr>
  
  <p>Notes:</p>
  
  <ul>
  <li>The activity in question is using the <code>singleTask</code> launchmode</li>
  <li>I have tried disabling the keyguard / lock but it has no effect</li>
  <li>I am witnessing this behavior on a Samsung Galaxy Tab 10.1 running Android 3.2. I have not tested whether this applies to anything else...</li>
  </ul>