/** * Distinguishes different kinds of app starts:
  • * * * * * @author williscool * inspired by * @author schnatterer * */ public enum AppStart { FIRST_TIME, FIRST_TIME_VERSION, NORMAL; } /** * The app version code (not the version name!) that was used on the last * start of the app. */ private static final String LAST_APP_VERSION = "1"; /** * Caches the result of {@link #checkAppStart(Context context, SharedPreferences sharedPreferences)}. To allow idempotent method * calls. */ private static AppStart appStart = null; /** * Finds out started for the first time (ever or in the current version). * * @return the type of app start */ public AppStart checkAppStart(Context context, SharedPreferences sharedPreferences) { PackageInfo pInfo; try { pInfo = context.getPackageManager().getPackageInfo( context.getPackageName(), PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); int lastVersionCode = sharedPreferences.getInt( LAST_APP_VERSION, -1); // String versionName = pInfo.versionName; int currentVersionCode = pInfo.versionCode; appStart = checkAppStart(currentVersionCode, lastVersionCode); // Update version in preferences sharedPreferences.edit() .putInt(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(TAG, "Unable to determine current app version from package manager. Defensively assuming normal app start."); } return appStart; } public 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(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; } }