In my android application ,user has to register by agreeing the terms and condition giving their email id. If user upgrade the application to next version, I should not get the
I use this piece of code to display the ChangeLog in my app to detect if the version has changed and display the ChangeLog accordingly. The same logic can be used for displaying or not displaying the Agreement, as the case may be.
These are my Global Declarations:
// FOR STORING VERSION CODE IN SHAREDPREFERENCES
private static final String PRIVATE_PREF = "my_app_name";
private static final String VERSION_KEY = "version_number";
try {
// GET THE PREFERENCE SET FOR THE CHANGELOG
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
final boolean showChanges = prefs.getBoolean("displayChangelog", false);
PackageInfo pkgInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
// SET THE CURRENT VERSION IN SHAREDPREFERENCES
SharedPreferences sharedPrefs = getSharedPreferences(PRIVATE_PREF, Context.MODE_PRIVATE);
// SAVED VERSION CODE
int savedVersionCode = sharedPrefs.getInt(VERSION_KEY, 0);String.valueOf(savedVersionCode));
// GET NEW VERSION CODE
int currentVersionCode = pkgInfo.versionCode;String.valueOf(currentVersionCode));
Editor editor = sharedPrefs.edit();
editor.putInt(VERSION_KEY, currentVersionCode);
editor.commit();
if (showChanges == false) {
/***** DISPLAY THE CHANGELOG *****/
// DISPLAY DIALOG WHEN THE APPLICATION IS INSTALLED FOR THE FIRST TIME
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("displayChangelog", true);
editor.commit();
} else if (showChanges == true && currentVersionCode > savedVersionCode) {
/***** DISPLAY THE CHANGELOG AFTER AN UPGRADE *****/
// DISPLAY DIALOG CHANGELOG AFTER AN UPGRADE
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("displayChangelog", true);
editor.commit();
}
} catch (Exception e) {
e.printStackTrace();
}
EDIT: Alongwith this code, I also use a Preferences
XML file to provide the use the ability to see the ChangeLog again and also store the state of the user preference to display the ChangeLog at next run. The XML for that: