tags:

views:

240

answers:

2

I'm creating a notification with something similar to the following:

    Intent ni = new Intent(this, SomeActivity.class);
    ni.putExtra("somestring", "somedata");
    PendingIntent contentIntent = 
        PendingIntent.getActivity(this, 0, ni, 
                PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_ONE_SHOT);
    Context context = getApplicationContext();

    notification.setLatestEventInfo(context, 
            res.getString(R.string.app_name), 
            text, contentIntent);

The key here is the extra data on the Intent for the notification. Once I click on the notification and it brings up SomeActivity, it brings up the activity and the extra data is available.

However, if I exit the app, hold the home button until the recent activities list comes up, and choose to open the app again, the extra data is still passed. Is there a way I can make this data get passed only if the app is opened via the Notification, and not from the recent activities list?

+1  A: 

Use a different Intent. For example, you could add an <intent-filter> for some custom action string, and use that for your Notification-based PendingIntent. In other words, you can't change the most-recent activities dialog, but you can change your side of the conflict.

CommonsWare
Now the recent activities dialog just shows the new PendingIntent... which means I have the same problem - the recent activities dialog still launches the same activity with the same extra data.
synic
That should only occur if the activity in question had been started from the `Notification`.
CommonsWare
I guess I don't understand. Even if I start an entirely different activity, and then launch my main activity from it, I'd still have to use extras for the logic, and they are still passed to the new activity. I tried making this "LauncherActivity" excludeFromRecents, but this just made the entire app be excluded, even though the main activity is specified to not exclude in the manifest.
synic
The "history" remembers what Intent was used to launch the task -- that could be from a `Notification`, from a launcher icon, or whatever. I was assuming, incorrectly I suspect, that your problem was distinguishing between a `Notification`-triggered `Intent` and one triggered from a launcher icon (as recorded in the history). If you are concerned about distinguishing between a `Notification`-launched `Intent` and a `Notification`-launched `Intent` as recorded in the history, use `FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY` (as you note in your answer) or rethink why you care about the difference.
CommonsWare
+1  A: 

Looks like I can check to see if the Activity was launched from the recent activities dialog with this:

if((getIntent().getFlags() & 
        Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
    // ignore extras
}
else {
    // do not ignore extras
}

I don't know if this is the best way, but it does work.

synic