I need to call a method (or start an activity, or something else) that will update a file containing data the app needs.
But I want it to be done only once, when the
to do something only once in app you need something like this :
boolean mboolean = false;
SharedPreferences settings = 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.Editor editor = settings.edit();
editor.putBoolean("FIRST_RUN", true);
editor.commit();
} else {
// other time your app loads
}
You can do it by using SharedPrefrences
. Look this:
SharedPreferences ratePrefs = getSharedPreferences("First Update", 0);
if (!ratePrefs.getBoolean("FrstTime", false)) {
// Do update you want here
Editor edit = ratePrefs.edit();
edit.putBoolean("FrstTime", true);
edit.commit();
}
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.
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.