Ernest Zamelczyk
Ernest Zamelczyk

Reputation: 2819

appWidgetIds only contain one id

I have appWidgetProvider that updates views of each widget located on homescreen and each widget has a button which triggers updating the view but apparently no matter which button I press on whichever widget only and always the one of them is being updated

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);

    for (int widgetId: appWidgetIds){
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);

        Intent update = new Intent(context, QuotesAppWidgetProvider.class);
        Bundle bundle = new Bundle();
        bundle.putIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{widgetId});
        update.putExtras(bundle);
        update.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, update, 0);

        views.setOnClickPendingIntent(R.id.widget_refresh, pendingIntent);
        appWidgetManager.updateAppWidget(widgetId, views);
    }
}

Let's say I have widget with id=30 and widget with id=31 and I click the refresh button on widget30 and then widget30 is being updated which is desired but when I click on refresh button on widget31 then widget30 is being updated for I don't know what reason.

Upvotes: 1

Views: 91

Answers (2)

Ernest Zamelczyk
Ernest Zamelczyk

Reputation: 2819

It took me a bit but I finally figured it out. All of the widgets had pendingintents with the same request code and since it was broadcast it overwritten all of them with one. This was the line that helped me:

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, widgetId, update, PendingIntent.FLAG_UPDATE_CURRENT);

Upvotes: 1

Kumar M
Kumar M

Reputation: 1014

Use this code and check.

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);

    for (int widgetId: appWidgetIds){
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);

        Intent update = new Intent(context, getClass());
        update.setAction("actUpdateWidgets");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, update, 0);

        views.setOnClickPendingIntent(R.id.widget_refresh, pendingIntent);
        appWidgetManager.updateAppWidget(widgetId, views);
    }
}


@Override
    public void onReceive(Context context, Intent intent)
    {
        super.onReceive(context, intent);

        if (intent.getAction().equalsIgnoreCase("actUpdateWidgets"))
        {
          Intent i = new Intent(context, QuotesAppWidgetProvider.class);
          i.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
          int ids[] = AppWidgetManager.getInstance(context).getAppWidgetIds(new                     ComponentName(context, QuotesAppWidgetProvider.class));
          i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
          context.sendBroadcast(i);
        }
    }

Upvotes: 0

Related Questions