3

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?

PhilYoussef
  • 303
  • 3
  • 11
  • Someone else seems to have run into this, but no answer yet [link](http://stackoverflow.com/questions/2882459/getextra-from-intent-launched-from-a-pendingintent) – PhilYoussef Jul 24 '11 at 21:36

2 Answers2

2

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);
            }
        }
    } 
user1818726
  • 101
  • 6
0

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

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • I already have the intent when inside the method onHandleIntent, so I'm not sure what calling getIntent will achieve? My problem is that intent.hasExtra("SomeExtra") evaluates to false. The extra isn't there. In fact, if i debug, the Intent Bundle map is null (meaning there are NO extras at all) – PhilYoussef Jul 24 '11 at 21:27
  • Hmm...try doing the same on another way. Try testing if there is `any` Bundle inside. `if(intent.getExtras()==null)` ... I know I'm running in circle, but you must experiment. – Nikola Despotoski Jul 24 '11 at 22:19
  • I finally solved it! I needed to do the following: final Intent originalIntent = (Intent)intent.getExtras().get( Intent.EXTRA_INTENT ); final String extra = originalIntent.getStringExtra("SomeExtra"); Any idea why the intent would be bundled.. in another intent? – PhilYoussef Jul 24 '11 at 22:30
  • Probably you start `PendingIntent` – Nikola Despotoski Jul 24 '11 at 22:31
  • The reason why intent is in another intent is: https://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT – Teng-pao Yu Oct 24 '17 at 08:20
  • @Teng-paoYu I don't quite see why the document for that flag explains this situation. The document only says the extras will be replaced. – renyuneyun Jul 19 '19 at 17:47
  • @renyuneyun To be honest I can't see why either now as well. It was quite a while ago and I can't differentiate was it I had a case using such a flag and have this reproduced or I was just totally confused (or pasted something incorrectly) at that moment. Sorry for not being able to help! – Teng-pao Yu Jul 21 '19 at 15:40