anks anks - 1 year ago 56
Android Question

Check if user has updated app or installed a fresh copy?

I will be releasing an update for my app with a new data structure, therefore if a user is updating my app I need to update their current data. So I was wondering how can I programatically check if the user updated my app or installed a new copy (if a new copy is installed I don't need to update anything) ?

Answer Source


If your data is in an SQLite database, you can implement code in SQLiteOpenHelper.onUpgrade().

Checking previous version

If not, you need some kind of way to check the previous version that was open on the device. Something like:

PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
// Use SharedPreferences to store something like "LAST_VERSION_RUN" = pInfo.versionName

Then, you can check if previous version is not equal to current version. Of course, this kind of code has to be in previous versions, also. There is no way to implement, once an app is already in the wild, a code that will detect whether an app has been upgraded.

Checking current version

I suppose what you could do in this case is detect the app version, and then set a flag to not update the data schema again. So, let's say your app version was 1.0.2, and is now 1.0.3:

PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
if (pInfo.versionName.equals("1.0.3") && /* some SharedPreference value */ == false) {
    // upgrade
    // set SharedPreference value to true

I think that's going to be the best way to implement this if you're not using SQLite.