My app has some widgets which, in the new version of the app, require some additional data. I get this data in MainApplication.java
, but I am worried that when the users update, MainApplication
will not run automatically if they don't manually open the app. However, since the widgets will already be on the homescreen, and since this data will be unavailable, widgets will crash.
Are my fears realistic or will MainApplication
ALWAYS run first (also before AppWidgetProvider.onUpdate()
is called), even after the app is automatically updated and the user does not it at all?
When your app is started, the constructor of your Application
implementation is called first and the Application::onCreate()
method is called right after. I'm assuming that's what you are asking when you say "will MainApplication
always run first?". Take a look at what the docs say about Application::onCreate()
(taken from here):
Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.
Also, this method won't be called when your app is updating and, as far as I know, it won't be called when your app finishes updating.
The AppWidgetProvider.onUpdate()
method, on the other hand, is called after the app is reinstalled. The code to upgrade your widgets should be placed inside it, so it will be executed after your app's upgrade.
public class WidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate (Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
//Update your widgets here
}
}