title: Activity opened twice
body: <p>I have an application that uses <a href="http://urbanairship.com/" rel="noreferrer">Urban Airship</a> for push notification. When a notification arrives and the user clicks on it, activity A in my application should open and do something.</p>
  
  <p>I've installed the <code>BroadcastReceiver</code> as is shown <a href="http://docs.urbanairship.com/build/android_features.html#custom-handling-of-push-events" rel="noreferrer">in the docs</a>, and it's <em>almost</em> working.</p>
  
  <ol>
  <li>When my app is in the foreground I don't let the user see the notification at all, and just handle it automatically.</li>
  <li>When my app is not running at all, the activity opens up just fine. </li>
  <li>When my app is in the background (which always happens when A is the top activity), <em>a second instance of Activity A is created</em>. </li>
  </ol>
  
  <p>This is, of course, a problem. I don't want two A activities, I just want one of them. Here's the relevant <code>BroadcastReceiver</code> code:</p>
  
  <pre><code>@Override
  public void onReceive(Context ctx, Intent intent)
  {
      Log.i(tag, "Push notification received: " + intent.toString());
      String action = intent.getAction();
      int notificationId = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, -1);
      if(action.equals(PushManager.ACTION_NOTIFICATION_OPENED))
      {
          Intent intentActivity = new Intent(ctx, ActivityA.class);
          intentActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          UAirship.shared().getApplicationContext().startActivity((intentActivity);
      }
  }
  </code></pre>
  
  <p>UPDATE:
  I tried to bypass this bug by calling <code>System.exit(0)</code> when the user presses Back on Activity A. The process ended, but then it was restarted immediately! My BroadcastReceiver is not called again in the second instance. What's happening?</p>
  
  <p>UPDATE 2:
  @codeMagic asked for more information about the app and activity A.</p>
  
  <p>This app lets its user review certain items and comment on them. Activity A is started when the app is launched. If the user's session isn't valid any more, a Login activity is started. Once the user logs in, activity A becomes active again. A only has a "No items to review" message and a "Try now" button.</p>
  
  <p>When the user logs in, the server starts sending push notifications whenever a new item is available for review. When the app gets the notification, activity A accesses the server and gets the next item to review. The item is shown in activity B. Once the review is submitted to the server, activity B finishes and activity A is again the top activity.</p>
  
  <p>The server knows when a user is reviewing an item (because activity A fetched it), and doesn't send push notifications until the review is submitted - meaning a notification can't come if the user isn't logged in or if the user is viewing activity B.</p>
  
  <p>While I agree there is a subtle race condition here, it is not causing the problem I'm seeing - in testing I am 100% positive there's no race condition - the push notification is only sent <em>after</em> Activity A becomes active again.</p>