Search code examples
androidandroid-appwidget

Android - Weird app widget behavior : it erases its content itself sometimes


I have been struggling all week with this issue.

I have an app widget that calls a Service to update itself.

Sometimes, after I re-run my app, the widget updates correctly on the background; however when I press back button, the widget content disappears, leaving only the default xml content.

Using debugger, AppWidgetProvider onUpdate is never called; sometimes this also happens after some time without using the phone; or turning wifi on and off.

It doesn't show the "error with widget" message. It is like the widget was recreated (memory problems?) but didn't execute the updateWidget methods.

Once I read that updating widget from different process Ids would result in ignoring the update, but I don't know how to test this.


Solution

  • The problem was that calling updateAllWidgets recreate the widget from the default.xml and then over that one updates the remoteviews.

    If only certain part of the widget is wished to be updated, over an already modified widget, partiallyUpdateAppWidget should be called.

    /** Update a given widget (id) with the given remoteViews instructions */
        protected static void updateWidget(Context context, RemoteViews remoteView, int widgetId){
            AppWidgetManager manager = AppWidgetManager.getInstance(context);
            manager.partiallyUpdateAppWidget(widgetId, remoteView);
        }