Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the intent from onCreate in Android

I launch an activity from my widget using an Intent with some extra, anyway I can only get the Intent when the activity is in background.. How can I get the Intent when activity is created? Tried with this.getIntent() but extras are null.

Thanks in advance

like image 300
Erenwoid Avatar asked Oct 03 '11 15:10

Erenwoid


People also ask

What is onCreate () meant for?

onCreate() It is called when the activity is first created.

How do you get an intent?

To handle the content delivered by an Intent , call getIntent() to get the Intent object. Once you have the object, you can examine its contents to determine what to do next.

Why do we need to call setContentView () in onCreate () of activity class?

As onCreate() of an Activity is called only once, this is the point where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.


2 Answers

How can I get the Intent when activity is created?

Call getIntent() on the Activity.

Tried with this.getIntent() but extras are null.

One possibility is that you are arranging for an existing instance of the activity to return to the foreground (e.g., including FLAG_ACTIVITY_REORDER_TO_FRONT), in which case you will need to override onNewIntent() and collect the Intent there.

Another possibility is that you originally created a PendingIntent for the Intent with no extras, then later tried to create a new PendingIntent on an equivalent Intent (e.g., identifying the same activity) and included extras. In that case, you need to include FLAG_UPDATE_CURRENT or FLAG_CANCEL_CURRENT when creating the PendingIntent, so your new/changed extras are taken into account.

like image 54
CommonsWare Avatar answered Sep 26 '22 00:09

CommonsWare


getIntent(); is the proper way to get the intent that launched the Activity. It is possible that extras are null. That meens, that there are no extras :)

Are you sure that you added your extras?

like image 30
Andras Balázs Lajtha Avatar answered Sep 24 '22 00:09

Andras Balázs Lajtha