Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to call method only on installation of App

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 app is first installed; because after I will handle the update of the file myself.

Please how can it be done?

Thanks for any suggestion

like image 394
Lisa Anne Avatar asked Feb 28 '13 11:02

Lisa Anne


People also ask

How do you call a method only once on Android?

You can do this, by using shared preferences . Store a value in shared preferences: SharedPreferences prefs = getPreferences(MODE_PRIVATE); SharedPreferences. Editor editor = prefs.

How can I run Android apps without installing?

With Google Play Instant, people can use an app or game without installing it first. Increase engagement with your Android app or gain more installs by surfacing your instant app across the Play Store and Google Play Games app.

Which method is called when app is closed?

The general rule is that either onDestroy() is called, or your process is terminated, or your code crashed.


3 Answers

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
}
like image 163
Marko Niciforovic Avatar answered Oct 17 '22 08:10

Marko Niciforovic


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();
        }
like image 37
baldguy Avatar answered Oct 17 '22 08:10

baldguy


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.

like image 1
rdrobinson3 Avatar answered Oct 17 '22 10:10

rdrobinson3