Mirko Benedetti Mirko Benedetti - 8 months ago 91
Android Question

Get intent extras if activity is running

I'm struggling with this problem: I have some notifications and I want to pass intent extras to an activity, even if the application is already running.

Some solutions I found here on S.O. didn't work for me, like adding:


to the manifest, I'm also using this:


like someone suggested.

My code is this:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(NotificationService.this);
mBuilder.setContentTitle("New website change.");
mBuilder.setContentText("Changed: " + url);

Intent notificationIntent = new Intent(getContext(), MainActivity.class);
Bundle b = new Bundle();
b.putString("key", url);


PendingIntent intent = PendingIntent.getActivity(getContext(), id, notificationIntent, 0);


NotificationManager mNotificationManager = (NotificationManager) getSystemService(NotificationService.this.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, mBuilder.build());

And then inside the activity:

Bundle b = getIntent().getExtras();
if(b != null) {
url = b.getString("key");

Any help will be welcome, I will release this app's code as open source, thanks.

Bundle b = getIntent().getExtras();

This will always return the Intent that was used to create the activity instance.

In cases where the activity already exists, and you are using Intent flags (e.g., FLAG_ACTIVITY_REORDER_TO_FRONT or manifest settings (e.g., singleTop) to direct control back to that activity, you need to override onNewIntent(). The Intent delivered to that method is the Intent that was used to bring the activity back to the foreground.