Question

I have multiple instances of a widget. When I click on a widget the configuration activity is started as expected, the activity retrieves the corresponding widget id (I followed this post explaining how to differentiate intents):

Intent configIntent = new Intent(context, WidgetConfigureDisplayActivity.class);
configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.mood_main_layout, pendingIntent);

appWidgetManager.updateAppWidget(appWidgetId, views);

So everything works fine except when I use the home button (and not the back one) on the configuration activity.
On home screen, if I click on another widget instance, the configuration activity is displayed but it is not updated with the new widget instance configuration (widget id in the extra is unchanged).

Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
  widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
      AppWidgetManager.INVALID_APPWIDGET_ID);
}

I believed the purpose of the FLAG_UPDATE_CURRENT flag was to update the widget id in the activity but it doesn't seem to work (I tried to override onNewIntent, onResume methods with no luck). Playing with launchMode & other flags doesn't help either.

Was it helpful?

Solution

Adding FLAG_ACTIVITY_CLEAR_TOP on config intent fixes the issue.

configIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

At this time I have only one activity so I don't know if it's the best solution or if this flag could break other navigation behaviour.
Any thoughts will be appreciated.

OTHER TIPS

when you pess back button activity is destroyed .And when you call it again from widget new instance of activity is called.But when you press home button activity is destroy ,it will remain in memory and old instance is called. So best practice is to add flag(FLAG_ACTIVITY_NEW_TASK) while calling activity from widget,service or broadcast.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top