Search code examples
androidkotlinandroid-appwidget

Why are my manually updated AppWidgets not updating?


I have two widgets: One is called "NoteWidget" and is a normal app widget that has a ConfigurationActivity. Whenever I set it up for the first time, it does call onUpdate(), but it never does that again.

The other one is the "UpcomingWidget", and this one is a Collection Widget, but because this one does not have a Configuration Activity, onUpdate() never gets called.

It is very weird, because it used to work when I only had the NoteWidget, and even when I was starting to implement the UpcomingWidget, but at some point it stopped working.

I update all my widgets manually with the method refreshWidgets(). It does get called,but onUpdate() does not in any widget provider class:

fun refreshWidgets(context: Context) {
    val manager = AppWidgetManager.getInstance(context)
    val noteIntent = Intent(context, NoteWidget::class.java)
    val upcomingIntent = Intent(context, UpcomingWidget::class.java)
    noteIntent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
    upcomingIntent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
    val noteIds = manager.getAppWidgetIds(ComponentName(context, NoteWidget::class.java))
    val upcomingIds = manager.getAppWidgetIds(ComponentName(context, UpcomingWidget::class.java))

    noteIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, noteIds)
    upcomingIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, upcomingIds)
    context.sendBroadcast(noteIntent)
    context.sendBroadcast(upcomingIntent)
}

AndroidManifest.xml

<receiver android:name=".widgets.NoteWidget" android:label="@string/note_widget">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/note_widget_info" />
        </receiver>

        <receiver android:name=".widgets.UpcomingWidget" android:label="@string/upcoming_widget">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/upcoming_widget_info" />
        </receiver>

        <service
            android:name=".widgets.UpcomingWidgetService"
            android:permission="android.permission.BIND_REMOTEVIEWS" />

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

note_widget_info.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialKeyguardLayout="@layout/note_widget"
    android:initialLayout="@layout/note_widget"
    android:minWidth="110dp"
    android:minHeight="110dp"
    android:minResizeHeight="110dp"
    android:minResizeWidth="110dp"
    android:previewImage="@drawable/example_appwidget_preview"
    android:resizeMode="horizontal|vertical"
    android:updatePeriodMillis="0"
    android:widgetCategory="home_screen"
    android:configure="com.byteseb.grafobook.activities.NotePicker"/>

upcoming_widget_info.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialKeyguardLayout="@layout/upcoming_widget"
    android:initialLayout="@layout/upcoming_widget"
    android:minWidth="110dp"
    android:minHeight="110dp"
    android:minResizeHeight="110dp"
    android:minResizeWidth="110dp"
    android:previewImage="@drawable/example_appwidget_preview"
    android:resizeMode="horizontal|vertical"
    android:updatePeriodMillis="0"
    android:widgetCategory="home_screen" />

Solution

  • As @MikeM mentioned in a comment, the issue was that I passed AppWidgetManager.EXTRA_APPWIDGET_ID as the name of the extra, when it should have been AppWidgetManager.EXTRA_APPWIDGET_IDS.