PendingIntent does not send Intent extras PendingIntent does not send Intent extras android android

PendingIntent does not send Intent extras


Using PendingIntent.FLAG_CANCEL_CURRENT not a good solution because of inefficient use of memory. Instead use PendingIntent.FLAG_UPDATE_CURRENT.

Use also Intent.FLAG_ACTIVITY_SINGLE_TOP (the activity will not be launched if it is already running at the top of the history stack).

Intent resultIntent = new Intent(this, FragmentPagerSupportActivity.class).                    addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  resultIntent.putExtra(FragmentPagerSupportActivity.PAGE_NUMBER_KEY, pageNumber);PendingIntent resultPendingIntent =                PendingIntent.getActivity(                        this,                        0,                        resultIntent,                        PendingIntent.FLAG_UPDATE_CURRENT                );

Then:

@Override    protected void onCreate(Bundle savedInstanceState) {        try {            super.onCreate(savedInstanceState);            int startPageNumber;            if ( savedInstanceState != null)            {                startPageNumber = savedInstanceState.getInt(PAGE_NUMBER_KEY);//so on

It should work now.


If you still have not expected behaviour, try to implement void onNewIntent(Intent intent) event handler, that way you can access the new intent that was called for the activity (which is not the same as just calling getIntent(), this will always return the first Intent that launched your activity.

@Overrideprotected void onNewIntent(Intent intent) {    int startPageNumber;    if (intent != null) {        startPageNumber = intent.getExtras().getInt(PAGE_NUMBER_KEY);    } else {        startPageNumber = 0;    }}


I think you need to update the Intent when you receive a new one by overriding onNewIntent(Intent) in your Activity. Add the following to your Activity:

@Overridepublic void onNewIntent(Intent newIntent) {    this.setIntent(newIntent);    // Now getIntent() returns the updated Intent    isNextWeek = getIntent().getBooleanExtra(IS_NEXT_WEEK, false);        }

Edit:

This is needed only if your Activity has already been started when the intent is received. If your activity is started (and not just resumed) by the intent, then the problem is elsewhere and my suggestion may not fix it.


Following code should work:-

int icon = R.drawable.icon;String message = "hello";long when = System.currentTimeMillis();NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);Notification notification = new Notification(icon, message, when);Intent notificationIntent = new Intent(context, MainActivity.class);notificationIntent.putExtra("isNexWeek", true);notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);notification.setLatestEventInfo(context, title, message, pIntent);notification.flags |= Notification.FLAG_AUTO_CANCEL;notificationManager.notify(0, notification);

In MainActivity onCreate:

if (getIntent().getExtras() != null && getIntent().getExtras().containsKey("isNextWeek")) {        boolean isNextWeek = getIntent().getExtras().getBoolean("isNextWeek");}