After rebooting or upgrading the app, my Android widget stops updating

Viewed 1645

Whenever I reboot my phone or upgrade the app (on my test device and on Android emulator) the widget stops updating until I create a new instance of the widget. Then both instances of the widget will start updating again. I assume it's something with calling the onUpdate() on old WidgetIds, but I can't figure it out. Here's a small snipped of my code.

 public class NewAppWidget extends AppWidgetProvider {

private static final String refresh = "b_refresh";

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);

    Intent intent = new Intent(context, NewAppWidget.class);
    intent.setAction(refresh);
    intent.putExtra("appWidgetId", appWidgetId);

    views.setOnClickPendingIntent(R.id.refresh, PendingIntent.getBroadcast(context,0,intent, PendingIntent.FLAG_UPDATE_CURRENT));
    appWidgetManager.updateAppWidget(appWidgetId, views);
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    for (int appWidgetId : appWidgetIds) {
        updateAppWidget(context, appWidgetManager, appWidgetId);
    }
}

@Override
public void onReceive(Context context, Intent intent) {
    if(refresh.equals(intent.getAction())) {
        Toast.makeText(context, "Clicked2", Toast.LENGTH_LONG).show();
    }
}

}

EDIT: Here's my manifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".NewAppWidget">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="android.appwidget.action.EXTRA_APPWIDGET_IDS"/>
        </intent-filter>

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

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

1 Answers

Add a call to super.onReceive() to your onReceive().

Explanation

If you look at the source for the base class onReceive(), you can see that it implements part of the framework logic for managing Widget lifecycle. (Also hinted at by the docs). It handles APPWIDGET_UPDATE and is, in fact, what's responsible for calling onUpdate() in the first place. (E.g., when the system boots up, and it needs to draw your initial widget, it sends your app an APPWIDGET_UPDATE, which gets passed to onReceive()). So, I'm not 100% sure how onUpdate() was ever getting called, in your case, but I assume you have some code somewhere else that calls updateAppWidget(), and that's the only reason your widgets appeared to work even momentarily.

Related