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?
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 :)
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With