Android: How To Call Method Only On Installation Of App
Solution 1:
to do something only once in app you need something like this :
booleanmboolean=false;
SharedPreferencessettings= getSharedPreferences("PREFS_NAME", 0);
mboolean = settings.getBoolean("FIRST_RUN", false);
if (!mboolean) {
// do the thing for the first time
settings = getSharedPreferences("PREFS_NAME", 0);
SharedPreferences.Editoreditor= settings.edit();
editor.putBoolean("FIRST_RUN", true);
editor.commit();
} else {
// other time your app loads
}
Solution 2:
You can do it by using SharedPrefrences
. Look this:
SharedPreferencesratePrefs= getSharedPreferences("First Update", 0);
if (!ratePrefs.getBoolean("FrstTime", false)) {
// Do update you want hereEditoredit= ratePrefs.edit();
edit.putBoolean("FrstTime", true);
edit.commit();
}
Solution 3:
Create a launcher activity that loads when your app starts. Have it check for a value (such as firstStartUp) in SharedPreferences (http://developer.android.com/guide/topics/data/data-storage.html#pref) . The first time you ever run the app this value will not exist and you can update your data. After your data is updated set the value in shared preferences so that your app finds it the next time it launches and will not attempt to update the data again.
Solution 4:
You could do it by, in your main activities onCreate method, checking your shared prefereces file for any arbitary boolean that you pick the name of. On your first launch this won't be there so you can then call your method and set your boolean to true. This means on the next start of your app this value would be true and the call to your function can be skipped.
Post a Comment for "Android: How To Call Method Only On Installation Of App"