How to update Android AppWidget after updating / reinstalling the application?

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

Unfortunately, after updating the application to the new version, users need to remove all the widgets that they created earlier and reinstall them. This is very annoying. However, I am convinced that there is a way.

After updating the application, the widgets will not be destroyed, they simply are in the "default" state, which they inherit from the layout.

Are there any of the four widget callbacks that I can use to update widgets? Is there an after-gradepgrade-callback that I can act on and update widgets?

+4
source share
2 answers

I did not understand that onUpdate is called after the application is reinstalled. Thus, the code simply goes into the update method and will be called after each update:

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

You can register BroadcastReceiver to receive ACTION_PACKAGE_REPLACED broadcast, and then notify your widgets in the receiver to reload (probably via AppWidgetManager.notifyAppWidgetViewDataChanged(..) ). You may also need to pass ACTION_MY_PACKAGE_REPLACED .

+1
source

All Articles