Skip to content

Instantly share code, notes, and snippets.

@RamPattikonda
Forked from williscool/AppStart.java
Created June 19, 2016 04:53
Show Gist options
  • Save RamPattikonda/83c356187750778bec290efa910ff671 to your computer and use it in GitHub Desktop.
Save RamPattikonda/83c356187750778bec290efa910ff671 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;
}
// ...
}
// ...
}
@RamPattikonda
Copy link
Author

I am getting error in AppStart.java. How can you create this java file without any class in it.

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