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.
Try using both of:
intent.setAction("actionstring" + System.currentTimeMillis());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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With