Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getIntent() Extras always NULL

I wrote a simple Android App that show a custom Notification like this:

Context context = getApplicationContext();           NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification( R.drawable.icon, title, System.currentTimeMillis());   Intent notificationIntent = new Intent( context,  this.getClass());  notificationIntent.putExtra("com.mysecure.lastpage", "SECURECODE");  PendingIntent pendingIntent = PendingIntent.getActivity( context , 0, notificationIntent, 0);                notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT; notification.contentView = new RemoteViews(context.getPackageName(), R.layout.notifypbar); notification.contentIntent = pendingIntent;  notification.contentView.setTextViewText(R.id.notifypb_status_text, text); notification.contentView.setProgressBar(R.id.notifypb_status_progress, 100, (int)(100*progress), false);  manager.notify(104, notification); 

This piece of code is called ONLY ONCE in my application and it displays a notification with a progress bar (all correctly).

Now, when a user clicks on this notification my application handles the onResume event.

public void onResume() {     super.onResume();     // TODO: Extras è SEMPRE NULL!!! impossibile!     Intent callingintent = getIntent();      Bundle extras = callingintent.getExtras(); 

but extras is always NULL!

I've tried any combination of:

notificationIntent.putExtra("com.mysecure.lastpage", "SECURECODE"); 

or

Bundle extra = new Bundle(); extra.putString(key, value); notificationIntent.putExtra(extra); 

but getIntent().getExtras() returns always NULL.

like image 269
Magius Avatar asked Jun 15 '11 02:06

Magius


People also ask

Can getIntent return null?

It can return null, but only if you set it to null in the Activity .

What is the use of getIntent ()?

you can retrieve this data using getIntent in the new activity: Intent intent = getIntent(); intent. getExtra("someKey") ... So, it's not for handling returning data from an Activity, like onActivityResult, but it's for passing data to a new Activity.

How do I know if my intent has extras Kotlin?

Use the Intent. hasExtra(String name) to check if an extra with name was passed in the intent. Also, use Intent. getStringExtra(String name) directly on the intent to handle the NullPointerException if no extras were passed.


1 Answers

This is the scenario:
The method getIntent() returns the FIRST intent than launch activity.

So, when the activity is CLOSED (terminated) and the user clicks on the notification, it will run a new instance of the activity and getIntent() works as expected (Extras is not null).

But if the activity is "sleeping" (it is in the background) and the user clicks on the notification, getIntent() always returns the very FIRST intent that started the activity and NOT the notification intent.

So to catch the notification intent while the application is running, simply use this

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

and then override onNewIntent(Intent newintent).

So when an application first runs, getIntent() can be used and when application is resumed from sleeping, onNewIntent works.

like image 56
Magius Avatar answered Sep 20 '22 19:09

Magius