I am new to android development and and I want to setup some of application’s attributes based on Application first run after installation. Is there any way to find that the application is running for the first time and then to setup its first run attributes?
Share
The following is an example of using
SharedPreferencesto achieve a ‘first run’ check.When the code runs
prefs.getBoolean(...)if there isn’t abooleansaved inSharedPreferenceswith the key “firstrun” then that indicates the app has never been run (because nothing has ever saved a boolean with that key or the user has cleared the app data in order to force a ‘first run’ scenario). If this isn’t the first run then the lineprefs.edit().putBoolean("firstrun", false).commit();will have been executed and thereforeprefs.getBoolean("firstrun", true)will actually return false as it overrides the default true provided as the second parameter.