Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect Android App has been upgraded from version x to y?

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.

like image 585
EV221 Avatar asked Sep 14 '25 12:09

EV221


1 Answers

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.

like image 141
Bryan Herbst Avatar answered Sep 16 '25 01:09

Bryan Herbst