Skip to content

Instantly share code, notes, and snippets.

@williscool
Created December 11, 2014 01:45
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save williscool/2a57bcd47a206e980eee to your computer and use it in GitHub Desktop.
Save williscool/2a57bcd47a206e980eee to your computer and use it in GitHub Desktop.
AppStart - Check if started for the first time Android
/**
* Distinguishes different kinds of app starts: <li>
* <ul>
* First start ever ({@link #FIRST_TIME})
* </ul>
* <ul>
* First start in this version ({@link #FIRST_TIME_VERSION})
* </ul>
* <ul>
* Normal app start ({@link #NORMAL})
* </ul>
*
* @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;
}
}
public class MainActivity extends Activity {
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// you dont actually need the context variable
// inside of an activity class you can pass it as this and that will work
// it just reads a bit better
context = this;
sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
switch (checkAppStart(this,sharedPreferences)) {
case NORMAL:
// We don't want to get on the user's nerves
break;
case FIRST_TIME_VERSION:
// TODO show what's new
break;
case FIRST_TIME:
// TODO show a tutorial
break;
default:
break;
}
// ...
}
// ...
}
@aquaflamingo
Copy link

👍

@icemc
Copy link

icemc commented Jun 25, 2017

Great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment