3

I want to create an app with two widget sizes (4x2 and 4x4).
For this I created two receivers:

    <receiver android:name=".widgets.CloudWidgetProviderLarge" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_large" />
    </receiver>

    <receiver android:name=".widgets.CloudWidgetProviderMedium" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_medium" />
    </receiver>

And one configuration activity:

<activity android:name=".widgets.WidgetConfigurationActivity">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
    </intent-filter>
</activity>

The two widgetProviders inherit from my abstract WidgetBaseProvider and only implement the different parts.

My problem is the following:
The onUpdate method of the concrete WidgetProvider class is called when the configuration activity is started, but not when the configuration is finished. And therefore not updating the widget when done with the configuration.

What I've tried so far in the WidgetConfigurationActivity:
This is not causing an update. Don't know what is wrong with it:

val appWidgetManager = AppWidgetManager.getInstance(this)
val views = RemoteViews(this.packageName, R.layout.widget_newscloud)
appWidgetManager.updateAppWidget(widgetData.widgetId, views)
val result = Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetData.widgetId)
setResult(Activity.RESULT_OK, result)
finish()

This works but obviously only for one widget kind:

val providerLarge = CloudWidgetProviderLarge()
providerLarge.onUpdate(this, appWidgetManager, intArrayOf(widgetId))
Marcel Bochtler
  • 1,291
  • 1
  • 14
  • 23

1 Answers1

0

After running into this myself, I had to do some research.

I started by observing that the onUpdate wasn't called after the config activity finished. I then found this answer to that. https://stackoverflow.com/a/12236443/2052088 But I had found that this did not fix it 100% of the time, and then I found this answer https://stackoverflow.com/a/44516844/2052088 completed the solution.

Combining the two:

Intent intent = new Intent(this, YourWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] ids = AppWidgetManager.getInstance(this)
        .getAppWidgetIds(new ComponentName(this, YourWidgetProvider.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
sendBroadcast(intent);

I know it has been over two years, but hopefully it helps someone.