8

I need to clear(equivalent to Clear Data in App Settings) all the old data in the app programmatically when the user updates the app from Google Play Store or any other sources. This is because I don't need any of the existing data from the old app since I have changed everything in the new app compared to the old one.

I thought of implementing a version check at the app startup, but I can't find a way to get the app's previous versionCode or versionName. The only way I figured out to clear the data is to check the lastUpdateTime at the time of publishing the app. But it's not reliable since the user has other ways or sources of getting the app (like sharing it with a friend or if the user had a backup of the old app).

Any Suggestions?

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
Joshua
  • 479
  • 6
  • 20
  • Did you find a solution for this? I want to achieve the same thing. – Kash Sep 10 '18 at 20:17
  • @kash I did not try any of the suggestions below since I was working in other things. But I believe atleast one of the solutions below will work – Joshua Sep 15 '18 at 05:29

3 Answers3

9

You can store versionCode in SharedPreferences and compare with current versionCode

For clear all data you just need to clear all value of SharedPreferences, Local Database and all local files which you have store.

public void clearData()
    {
        try {
            PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            int mCurrentVersion = pInfo.versionCode;
            SharedPreferences mSharedPreferences = getSharedPreferences("app_name",  Context.MODE_PRIVATE);
            SharedPreferences.Editor mEditor = mSharedPreferences.edit();
            mEditor.apply();
            int last_version = mSharedPreferences.getInt("last_version", -1);
            if(last_version != mCurrentVersion)
            {
                //clear all your data like database, share preference, local file 
                //Note : Don't delete last_version value in share preference
            }
            mEditor.putInt("last_version", mCurrentVersion);
            mEditor.commit();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

Note : Don't delete last_version value in share preference.

Niranj Patel
  • 35,286
  • 11
  • 96
  • 128
  • Your answer will work only if I had saved the version in Shared Preferences in the old app. Unfortunately I have not saved the version in any of the storages. – Joshua Jan 30 '18 at 11:30
  • @Joshua still you can do, if value is null then you can clear all your data. – Niranj Patel Jan 30 '18 at 11:34
  • @DrilonBlakqori just need to change that condition. – Niranj Patel Jan 30 '18 at 11:37
  • Yes i messed it up, `if(mSharedPreferences.getInt("last_version", -1) != mCurrentVersion)` should do it – Drilon Blakqori Jan 30 '18 at 11:38
  • @Joshua check my updated answer, it will also work for old app – Niranj Patel Jan 30 '18 at 11:39
  • @Joshua [if found solution then you can accept right answer so it will help to other user in future](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Niranj Patel Apr 05 '18 at 10:53
  • @Niranj ya sure. I still didn't test it out. I had some other features to be done. Once I test it and get it right, I'll surely update the status. – Joshua Apr 05 '18 at 13:50
3

You can simply use a PACKAGE_REPLACED receiver which gets fired whenever a package is replaced in your phone (also happens when updating an app). Declare it in your manifest:

<receiver android:name=".UpdateReciever">
    <intent-filter>
        <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
        <data android:scheme="package" android:path="com.my.app" />
    </intent-filter>
</receiver>

And add your receiver class. onReceive will get called when the app is updated:

public class UpdateReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context con, Intent intent) {

    }
}

EDIT: you don't need to filter the package. Use MY_PACKAGE_REPLACED instead which will fire only for your app.

Drilon Blakqori
  • 2,586
  • 2
  • 14
  • 25
  • Will the receiver get registered as soon as the update is installed? Because I assume that it will be registered when the user opens the app atleast once(since the previous app doesn't have this receiver). – Joshua Jan 30 '18 at 11:46
0

You can listen to a system broadcast with following intent and filter in onReceive with your package name to see if your app is updated.

<intent-filter>
   <action android:name="android.intent.action.PACKAGE_REPLACED" />
   <data android:scheme="package" />
</intent-filter>
Ali
  • 1,352
  • 1
  • 13
  • 16