I have a some data which am storing in preferences.If any updates found in PlayStore.I need to listen to the update action and have to clear the caches of myapp.
Asked
Active
Viewed 1,815 times
6
-
refer http://stackoverflow.com/questions/23908189/clear-cache-in-android-application-programmatically – sasikumar Feb 22 '16 at 06:56
-
1I know how clear the caches. I want to check the first launch of the New Updated version... – Raja Jawahar Feb 22 '16 at 07:01
-
much simpler: https://stackoverflow.com/a/76526011/1397821 – M. Usman Khan Jun 21 '23 at 19:01
2 Answers
2
To know if your app has been updated :
Store the current version code in shared preferences, and then use the below function in the main activity.
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);
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 enum here:
public enum AppStart {
FIRST_TIME,
FIRST_TIME_VERSION,
NORMAL
}
After that, to clear the cache : https://stackoverflow.com/a/23908638/1594776
-
Thankyou so much for reply. I am ok with that..But I just want to know,is there any separate action listeners for this one??? – Raja Jawahar Feb 22 '16 at 07:07
-
1There's an action i guess, ACTION_PACKAGE_REPLACED. you'll have to implement a receiver that listens to this action. IMO, storing version code is simpler. – Yash Feb 22 '16 at 07:15
-
I know that is simpler. I thought there should any alternatives to this one.. Anyway thanks a lot.. – Raja Jawahar Feb 22 '16 at 07:28
0
We have to handle it internally. Here is how I am handling it in a simple way.
For any version if it is required to clear data, I create a field with app versionCode to clear data for in gradle file:
defaultConfig {
minSdkVersion 24
targetSdkVersion 33
buildConfigField("int", "CLEAR_DATA_VER", "123")
}
In my first Activity, I check if data is cleared for the version or not:
...
val dataClearedVersion = Constants.dataClearedVersion(context)
if (dataClearedVersion == null || dataClearedVersion < BuildConfig.CLEAR_DATA_VER ){
Constants.cleanSharedPreferences(context)
Constants.setDataClearedVersion(context)
}
...
fun dataClearedVersion(context: Context): Int? {
val sharedPref = context.applicationContext.getSharedPreferences(PREF_KEY, Context.MODE_PRIVATE)
val version = sharedPref.getInt(APP_DATA_CLEARED_K, -1)
return if (version == -1) null else version
}
fun setDataClearedVersion(context: Context) {
val sharedPref = context.applicationContext.getSharedPreferences(PREF_KEY, Context.MODE_PRIVATE).edit()
sharedPref.putInt(APP_DATA_CLEARED_K, BuildConfig.CLEAR_DATA_VER).apply()
}

M. Usman Khan
- 3,689
- 1
- 59
- 69