3

Given two identical widgets with different data, A and B, when the data for widget A is shown in the activity launched by the widget, the back button is pressed and then widget B is pressed it all works fine. However, when the data for widget A is shown, the home button is pressed and then widget B is pressed, the data for widget A is still shown. From Android - When launch the same activity from widget with different extras, how to prevent the same instance show up after returned from HOME button? it suggests that my problem is that both PendingIntents (for widgets A and B) are the same and differ only in extras and are therefore cached. I.e. in the extras is the widget id which is the key I need to retrieve all the data for the widget-press action. One suggestion was to use different data, but I can't seem to get this to work. Here is the code:

Intent intent = new Intent(context, WidgetActivity.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.withAppendedPath(Uri.parse("droidln://widget/id/"),
               String.valueOf(appWidgetId)));
PendingIntent pendingIntent = 
               PendingIntent.getActivity(context, appWidgetId, intent, 0);

Adding the setData to the intent produced no difference. Any ideas on how to fix my problem of cached pending intents? I've also tried:

intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

and

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

and

intent.setAction("actionstring" + System.currentTimeMillis());

all to no effect.

3
  • Have you tried changing your last param of PendingIntent.getActivity from 0 to FLAG_UPDATE_CURRENT? Oct 31, 2011 at 22:38
  • @Ian, perhaps I don't understand it enough, but won't that override other pendingIntents (from other similiar widgets) extras? I.e. essentially do what I'm trying to avoid? In any case, it hasn't fixed my problem. Oct 31, 2011 at 22:46
  • It will override any PendingIntent that also matches. Your action should consist of your package name plus an "action" string of some kind plus the widget ID. That way, if the PendingIntent for a given widget updates, it will update the existing PendingIntent for that widget. Oct 31, 2011 at 22:57

2 Answers 2

5

Try using both of:

  1. Different action for pending intents - intent.setAction("actionstring" + System.currentTimeMillis());
  2. Proper flag - PendingIntent.FLAG_UPDATE_CURRENT (for your case : PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

I'm using this PendingIntent style in many applications and it proved to be right.

The sample code I've been using:

final ComponentName receiverName = new ComponentName(context, WidgetProvider.class);
Intent doSth = new Intent("REFRESH_ME" + System.currentTimeMillis());
doSth.setComponent(receiverName);
doSth.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
PendingIntent doSthPending = PendingIntent.getBroadcast(context, 0, doSth, PendingIntent.FLAG_UPDATE_CURRENT);
2
  • 1
    thanks, but unfortunately this didn't work for me. Are any manifest changes required for this stuff? Oct 31, 2011 at 23:09
  • 1
    Afraid its still not working for me. Thanks for your answer anyway. Nov 1, 2011 at 10:31
3

Finally (!) found the answer I was looking for, and it was as simple as I hoped it would be. The PendingIntents were getting setup and passed around correctly. However, I wasn't explicitly doing anything with the new intent. When the home button is pressed, the activity retains the original intent which launched it. All I needed to do was overwrite it with the new intent like this in my Activity:

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    super.onNewIntent(intent);
}

Then, the rest of the lifecycle kicks in (including onResume() where much of the processing in my Activity takes place) using the new intent, instead of the original one.

1
  • This worked for me! There was not need to set different Data or Action for the Intent, like other posts said, or even setting the FLAG_xxxxxxx parameter.
    – Miguel
    Apr 29, 2016 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.