Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 772
jones Avatar asked Aug 06 '12 21:08

jones


2 Answers

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

}
like image 185
jones Avatar answered Nov 15 '22 12:11

jones


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.

like image 30
Alex Curran Avatar answered Nov 15 '22 11:11

Alex Curran