Search code examples
androidandroid-appwidgetandroid-remoteviewandroid-appwidget-list

partiallyUpdateAppWidget on RemoteViews not working


In the RemoteViews for an AppWidget I have an AdapterViewFlipper which is supposed to flip when the user clicks a button in the AppWidget. According to the official documentation this should be done by calling the showNext on the RemoteViews. The AppWdiget should then be updated with partiallyUpdateAppWidget which even has the showNext() function as an example in the documentation. I've tried implementing this and I'm most likely making some silly mistake that I can't seem to figure out.

public class WidgetProvider extends AppWidgetProvider {

    public static final String NEXT = "next";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for (int appWidgetId : appWidgetIds) {
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

            Intent nextIntent = new Intent(context, WidgetProvider.class);
            nextIntent.setAction(NEXT);
            views.setOnClickPendingIntent(R.id.next_button,
                    PendingIntent.getBroadcast(context, appWidgetId, nextIntent,
                            PendingIntent.FLAG_UPDATE_CURRENT));

            views.setRemoteAdapter(R.id.view_flipper, new Intent(context,
                    WidgetRemoteViewService.class));

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(NEXT)) {
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.widget_layout);
            remoteViews.showNext(R.id.view_flipper);
            AppWidgetManager.getInstance(context).partiallyUpdateAppWidget(
                    AppWidgetManager.getInstance(context).getAppWidgetIds(
                            new ComponentName(context, WidgetProvider.class)),
                            remoteViews);
        }
        super.onReceive(context, intent);
    }
}

When replacing partiallyUpdateAppWidget with updateAppWidget the next button is working until the orientation changes or the phone goes to sleep which forces a redraw of the last stored RemoteViews. This leads me to believe the mistake I'm making has to do with partial update but I can't figure out what I'm doing wrong.


Solution

  • In which API level are you testing?

    Maybe the problem with withpartiallyUpdateAppWidget is related to this open issue issuetracker.google.com/issues/37136552