Question

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?

Was it helpful?

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 :)
  }

}

OTHER TIPS

You could register a BroadcastReceiver to receive the ACTION_PACKAGE_REPLACED broadcast, and then in the receiver notify your widgets to reload (likely through AppWidgetManager.notifyAppWidgetViewDataChanged(..)). You may also need to receive the ACTION_MY_PACKAGE_REPLACED broadcast as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top