Do I want to clear caches when my application is updated on the PlayStore in Android?

I have some data that is stored in the settings. If any updates found in PlayStore.I need to listen to the update action and clear myapp caches.

+4
source share
1 answer

To find out if your application is updated:

Save the current version code in the general settings, and then use the following function in the main action.

public static AppStart checkAppStart(Context context, SharedPreferences sharedPreferences) {
    PackageInfo pInfo;
    AppStart appStart = AppStart.NORMAL;
    try {
        pInfo = context.getPackageManager().getPackageInfo(
                context.getPackageName(), PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
        int lastVersionCode = sharedPreferences.getInt(
                Constants.LAST_APP_VERSION, -1);
        // String versionName = pInfo.versionName;
        int currentVersionCode = pInfo.versionCode;
        appStart = checkAppStart(currentVersionCode, lastVersionCode);

        // Update version in preferences
        sharedPreferences.edit()
                .putInt(Constants.LAST_APP_VERSION, currentVersionCode).commit(); // must use commit here or app may not update prefs in time and app will loop into walkthrough
    } catch (PackageManager.NameNotFoundException e) {
        Log.w(LOG_TAG,
                "Unable to determine current app version from package manager. Defensively assuming normal app start.");
    }
    return appStart
}

private static AppStart checkAppStart(int currentVersionCode, int lastVersionCode) {
    if (lastVersionCode == -1) {
        return AppStart.FIRST_TIME;
    } else if (lastVersionCode < currentVersionCode) {
        return AppStart.FIRST_TIME_VERSION;
    } else if (lastVersionCode > currentVersionCode) {
        Log.w(LOG_TAG, "Current version code (" + currentVersionCode
                + ") is less then the one recognized on last startup ("
                + lastVersionCode
                + "). Defensively assuming normal app start.");
        return AppStart.NORMAL;
    } else {
        return AppStart.NORMAL;
    }
}

AppStart is just an enumeration:

public enum AppStart {
FIRST_TIME,
FIRST_TIME_VERSION,
NORMAL

}

Then clear the cache: fooobar.com/questions/195225 / ...

+1
source

Source: https://habr.com/ru/post/1629851/


All Articles