Search code examples
androidwidgetandroid-pendingintent

Configure Widgets with difference content


My app creates many widgets of the same type, but with different content. When the user clicks on one widget, it is important to know the widgetId, so I know the content to update.

My way seems to be totaly wrong, because my PendingIntent gets overridden. So I always get the same EXTRA_APPWIDGET_ID instead of n different ones.

Here is my code, the part when I set the PendingIntent for my OnClick.

// edit intent
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.base_info_widget);
Intent editIntent = new Intent(context,MainActivity.class);
editIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TOP);         
editIntent.putExtra(GlobalVariables.COMMAND_KEY_ACTION,GlobalVariables.COMMAND_VALUE_EDIT);
editIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId);
Log.d(MY_TAG, "putExtra EXTRA_APPWIDGET_ID="+ Integer.toString(appWidgetId));
PendingIntent pendingEditIntent = PendingIntent.getActivity(context, 0, editIntent, PendingIntent.FLAG_CANCEL_CURRENT);
views.setOnClickPendingIntent(R.id.layout_wrapper, pendingEditIntent);

I hope my question was clear. How can I solve that problem?


Solution

  • Make your intent unique:

     editIntent.setData(Uri.parse(editIntent.toUri(Intent.URI_INTENT_SCHEME)));
    

    That's why you get always same id on all widgets