0

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.

Sander
  • 808
  • 6
  • 18
  • Try calling with`partiallyUpdateAppWidget` just for a specific widgetid to see if it helps. in the `nextIntent` put an extra with the appwidget id and then retrieve it in `onReceive` – Filipe Batista Sep 19 '17 at 20:15
  • I've tried your suggestion and unfortunately it has the same issue as updating all widgets at once. – Sander Sep 20 '17 at 08:27
  • 1
    In which API level are you testing? Maybe the problem with `withpartiallyUpdateAppWidget` is related to this open issue https://issuetracker.google.com/issues/37136552 – Filipe Batista Sep 20 '17 at 08:53
  • You are absolutely correct, this open issue is the problem. I was testing on API level 24 and my code is indeed running fine on lower API levels. Thanks for pointing it out, if you post it as an answer I'll accept it. – Sander Sep 20 '17 at 10:15

1 Answers1

2

In which API level are you testing?

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

Filipe Batista
  • 1,862
  • 2
  • 24
  • 39