PhilYoussef
PhilYoussef

Reputation: 313

Intent extra data seems to disappear when calling getExtra

For some reason, the bundled extra data I'm adding to my intent is not persisting when the intent is received. I've been debugging for quite some time now, but I'm not finding any issues with my code. Perhaps someone can help.

Intent Creation:

// Intent created within an IntentService for an AppWidgetProvider
final Intent textViewIntent = new Intent(this, LocWidgetProvider.class);
textViewIntent.setAction(ACTION_CHANGE_LOCALE);
textViewIntent.putExtra("SomeExtra", "SomeValue");
Log.d("ExtraTest", String.format("Extra data: %s", 
       textViewIntent.getStringExtra("SomeExtra")));
final PendingIntent textViewPendingIntent = PendingIntent.getBroadcast(this, 0,
                            textViewIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.SomeButton, textViewPendingIntent);

When Receiving the inent after pressing "SomeButton":

protected void onHandleIntent(Intent intent) {   
if(intent.getAction().equals(ACTION_CHANGE_LOCALE))
{
    if(!intent.hasExtra("SomeExtra"))
    {
        Log.d("ExtraTest", "Extra data was null :(");
    }
    else {
        String newLocale = (String)intent.getExtras().get("SomeExtra");
        LocaleManager.ChangePhoneLocale(new Locale(newLocale));
    }
}

I keep hitting:

"Extra data: SomeValue" (Intent seems to have the data when it is created)

"Extra data was null :(". (Intent no longer has the ExtraData when received

Am I doing something wrong when creating the intent?

Upvotes: 3

Views: 4099

Answers (2)

user1818726
user1818726

Reputation: 131

I have this problem also and solved it by getting the parent Intent.
In my case I'm setting two vales and one is forwarded to the new Activity and one is not.

Calling Activity;

                                intent.putExtra(Constants.EXTRA_ID, note.getFormatedDate());
                            intent.putExtra(Constants.EXTRA_NAME, name);

Receiving Activity

        String id = intent.getStringExtra(Constants.EXTRA_ID);


    if( id == null ) {
        Activity p = getParent();
        if( p != null ) {
            Intent i2 = p.getIntent();
            if( i2 != null ) {
                id = i2.getStringExtra(Constants.EXTRA_ID);
            }
        }
    } 

Upvotes: 2

Nikola Despotoski
Nikola Despotoski

Reputation: 50588

Try getParent().getIntent().getExtras().getWHATYOUWANT(key); since getParent() gets the activity that called the intent, so do the extras are shipped.

or String newLocale = intent.getExtras().getString("SomeExtra"); without casting

Upvotes: 0

Related Questions