How to create an app widget with a configuration activity, and update it for the first time?

前端 未结 4 735
自闭症患者
自闭症患者 2020-12-12 22:42

This is driving me crazy. I don\'t know how to update the app widget from the configuration activity, even with the recommended practises. Why the update method is not calle

4条回答
  •  醉话见心
    2020-12-12 23:23

    The drawback of doing the update through the AppWidgetManager is that you have to provide the RemoteViews which - from a design point of view - doesn't make sense as the logic related to RemoteViews should be encapsulated within the AppWidgetProvider (or in your case in the RemoteViewsService.RemoteViewsFactory).

    SciencyGuy's approach to expose the RemoteViews logic via a static method is one way to deal with that but there's a more elegant solution sending a broadcast directly to the widget:

    Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, ChecksWidgetProvider.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] {mAppWidgetId});
    sendBroadcast(intent);
    

    As a consequence the AppWidgetProvider's onUpdate() method will be called to create the RemoteViews for the widget.

提交回复
热议问题