Skip to content

Instantly share code, notes, and snippets.

@adennie
Created October 22, 2013 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adennie/7107587 to your computer and use it in GitHub Desktop.
Save adennie/7107587 to your computer and use it in GitHub Desktop.
detect first run after install/upgrade
protected void handleNewInstallsAndUpgrades() {
int savedVersion = readVersionCodeFromPref(PREF_TAG_CURRENT_APP_VERSION);
if (savedVersion == -1)
processNewInstall(getVersionCode());
else if (savedVersion != getVersionCode())
processUpgrade(getVersionCode(), readVersionCodeFromPref(PREF_TAG_PREVIOUS_APP_VERSION));
}
protected void processNewInstall(final int newVersionCode) {
updateSavedVersionCodes();
}
protected void processUpgrade(final int newVersionCode,
final int prevInstallVersionCode) {
updateSavedVersionCodes();
}
private void updateSavedVersionCodes() {
// copy the previous value of the version code for later reference before we overwrite it
saveVersionCodeToPref(PREF_TAG_PREVIOUS_APP_VERSION, readVersionCodeFromPref(PREF_TAG_CURRENT_APP_VERSION));
// save the current version code so it can be used by the line of code above, the next time we upgrade.
saveVersionCodeToPref(PREF_TAG_CURRENT_APP_VERSION, getVersionCode());
}
private void saveVersionCodeToPref(final String prefTag,
final int versionCode) {
mSharedPrefHelper.setLongNoStrict(this, prefTag, versionCode);
}
private int readVersionCodeFromPref(final String prefTag) {
return (int) mSharedPrefHelper.getLongNoStrict(this, prefTag, -1);
}
private int getVersionCode() {
int result = 0;
try {
result = getApplicationContext().getPackageManager().getPackageInfo(
getApplicationContext().getPackageName(), 0).versionCode;
} catch (NameNotFoundException e) {
// squelch
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment