Search code examples
androidandroid-appwidgetappwidgetprovider

How do I update an Android AppWidget after upgrading/reinstalling the application?


I created an App (https://play.google.com/store/apps/details?id=com.widget.analytics) that displays one's Google Analytics statistics on the Android home screen using an AppWidget.

Unfortunately, after upgrading the app to a new release the users need to remove all widgets they created before and have to install them again. This is very annoying. Yet, I am convinced that there is a way around.

After upgrading the app the widgets are not destroyed, they are just in their "default" state which they inherit from the layout.

Is there any of the four widget callbacks that I can use to update the widgets? Is there an after-ùpgrade-callback that I can act upon and update the widgets?


Solution

  • I did not realize that onUpdate is called once the app is reinstalled. So the code just goes into the update method and will be called after each upgrade:

    public class WidgetProvider extends AppWidgetProvider {
    
      @Override
      public void onUpdate (Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
        // Here we update the widget :)
      }
    
    }