jones
jones

Reputation: 665

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?

Upvotes: 8

Views: 2377

Answers (2)

jones
jones

Reputation: 665

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

}

Upvotes: 4

Alex Curran
Alex Curran

Reputation: 8828

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.

Upvotes: 1

Related Questions