title: Bring task to front on android.intent.action.USER_PRESENT
body: <p><strong>BACKGROUND</strong><br>
  I have a task (i.e. app) with multiple activities. </p>
  
  <p><strong>QUESTION</strong><br>
  How do I bring a task to the front w/o re-ordering the activity stack for that task? </p>
  
  <p><strong>USER SCENARIO</strong><br>
  When the device boots (i.e. after android.intent.action.BOOT_COMPLETED broadcast is received) my app starts, call this task 1, and displays activity A (code below). As the user interacts with task 1, he/she opens activity B on the stack (activity B is now currently displayed on screen). Next the user taps the home key and opens some other task, call it task 2, then locks the screen. The user unlocks the screen, and the android.intent.action.USER_PRESENT intent is broadcast and received by my app (see manifest snippet below). My executes the startActivity() call in my IntentReceiver class as expected, but instead of just bringing the task to the foreground it creates a new task, call it task 3. </p>
  
  <p><strong>CHANGES I'VE TRIED</strong><br>
  If I modify or change the Intent.FLAG_ACTIVITY_NEW_TASK to any other intent then I get this error message:</p>
  
  <blockquote>
    <p>Calling startActivity() from outside of an Activity  context requires
    the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?</p>
  </blockquote>
  
  <p>...so it looks like I have to use Intent.FLAG_ACTIVITY_NEW_TASK.</p>
  
  <p>If I change the main activity's lauchMode to "singleTask" the correct task is brought to the foreground (i.e. no new task is created), but the activity stack is reordered such that activity_A is on top of the stack.</p>
  
  <p>At this point I am at a loss as to how to bring an existing task to the foreground w/o re-ordering the activity stack while listening for the android.intent.action.USER_PRESENT intent, any help would be appreciated.</p>
  
  <p>BTW, This app is being delivered on to a group of employees, not the general public, on company owned android devices.</p>
  
  <p><strong>CODE SNIPPETS</strong><br>
  To start the app I setup a broadcast receiver in my manifest file. </p>
  
  <pre><code>&lt;application
         :
      &lt;activity
          android:label="@string/app_name"
          android:launchMode="singleTop"
          android:name=".activities.activity_A" &gt;
          &lt;intent-filter &gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.HOME" /&gt;
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
         :
      &lt;receiver
          android:enabled="true"
          android:name=".utilities.IntentReceiver"
          android:permission="android.permission.RECEIVE_BOOT_COMPLETED" &gt;
          &lt;intent-filter &gt;
              &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
              &lt;action android:name="android.intent.action.USER_PRESENT" /&gt;
  
              &lt;category android:name="android.intent.category.DEFAULT" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  </code></pre>
  
  <p>In my IntentReceiver class, I start my main activity...</p>
  
  <pre><code>public class IntentReceiver extends BroadcastReceiver {
  
  @Override
  
      public void onReceive(Context context, Intent intent) {
          Intent i = new Intent(context, activity_A.class);  
          i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(i);  
      }
  }
  </code></pre>