Run code only once after an application is installed on Android device

  1. Check if boolean X is True in shared preferences
  2. If not:
    a. Run the special code
    b. Save x as true in shared preferences

For example:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("firstTime", false)) {
    // run your one time code
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("firstTime", true);
    editor.commit();
}

Leave a Comment