Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity opened twice

I have an application that uses Urban Airship for push notification. When a notification arrives and the user clicks on it, activity A in my application should open and do something.

I've installed the BroadcastReceiver as is shown in the docs, and it's almost working.

  1. When my app is in the foreground I don't let the user see the notification at all, and just handle it automatically.
  2. When my app is not running at all, the activity opens up just fine.
  3. When my app is in the background (which always happens when A is the top activity), a second instance of Activity A is created.

This is, of course, a problem. I don't want two A activities, I just want one of them. Here's the relevant BroadcastReceiver 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);
    }
}

UPDATE: I tried to bypass this bug by calling System.exit(0) 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?

UPDATE 2: @codeMagic asked for more information about the app and activity A.

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.

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.

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.

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 after Activity A becomes active again.

like image 703
zmbq Avatar asked May 01 '13 21:05

zmbq


1 Answers

The solution was to add a launchMode='singleTask' to the activity in AndroidManifest.xml . As a result, instead of a new activity, onNewIntent of the same activity instance is called.

like image 87
zmbq Avatar answered Oct 10 '22 08:10

zmbq