I have a problem with my android app for own educational reason.
Is there a ready solution to detect that the app has now been updated from version x to y? Because I want to migrate some data once only if the app was installed in a special version before.
Thanks.
The easiest way is to store the last known version of your application in SharedPreferences, and check that when the app is launched.
You can get the current version code of your application with BuildConfig.VERSION_CODE
.
For example:
@Override
public void onStart() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int lastKnownVersion = prefs.getInt("lastKnownAppVersion", 0);
if (lastKnownVersion < BuildConfig.VERSION_CODE) {
// Your app has been updated
prefs.edit().putInt("lastKnownAppVersion", BuildConfig.VERSION_CODE);
}
}
Registering a BroadcastReceiver to listen for the ACTION_MY_PACKAGE_REPLACED
intent will tell you when your package is replaced (i.e. updated), but that won't tell you what version was previously installed.
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