1

In order to call some method just once in the app's lifecycle, not each time the app is launched, where should I place such method?

In onCreate() or somewhere else?

sandalone
  • 41,141
  • 63
  • 222
  • 338
  • 1
    Create a variable in shared preferences that counts app open times then if 0 you call the method Happy coding :D – PedroAGSantos Sep 01 '11 at 15:30
  • It is unclear how frequently you want this to be called. Once in the entire lifetime that the app is on the phone? Or once per time the app is brought to life. The android lifecycle manager can kill an app to make space for another. If this is the case, should the method be run again next time the app is started? – nicholas.hauschild Sep 01 '11 at 15:32
  • Once during the app's lifecycle. I thought this is from the moment the app starts until it is forcedly destroyed, but the lower answers confused me. – sandalone Sep 02 '11 at 14:42

3 Answers3

6

It should be in Application.onCreate() guarded by some SharedPreference boolean variable.

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(prefs.getBoolean("firstRun", true)) {            
    once(); // <-- your function
    prefs.edit().putBoolean("firstRun", false).commit();
}
inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • Now, I am confused. I though the app's life-cycle is from the moment it starts until the moment it is destroyed (mobile shutdown, Android OS kills it, etc.). In your code, this method will be called once EVER. Or I am wrong?! – sandalone Sep 02 '11 at 14:41
  • The `onCreate()` will be called each time application is started. But `once()` method will be called once per installation (or as refer to this once in lifetime). – inazaruk Sep 02 '11 at 14:44
  • This is not helping..as when you clear application data from the application manager then too it will take as a new life cycle even though app didn't get uninstalled – Rauf Sep 25 '13 at 11:41
  • @Rauf, clearing application data can be considered as uninstall-reinstall flow. There is little you can do to differentiate these two events. – inazaruk Sep 25 '13 at 17:11
  • @inazaruk hmm..i am trying to find that little only..see following : http://stackoverflow.com/questions/19005821/run-code-only-once-after-a-fresh-installation-shared-preference-is-not-a-solutio – Rauf Sep 26 '13 at 07:06
  • @Rauf, here is the tip how you can achieve this: use setComponentSettingEnabled for a disabled component. The component will stay enabled even if "clear data" was called. This is an undocumented behavior and can be changed at any time in future releases (might even be fixed in 4.3, didn't test it). http://developer.android.com/reference/android/content/pm/PackageManager.html#setComponentEnabledSetting – inazaruk Sep 27 '13 at 03:42
  • thanks @inazaruk please can you explain "use setComponentSettingEnabled for a disabled component" in context of my scenario please – Rauf Sep 27 '13 at 06:20
2

You can add it to onCreate() and only call the method if it hasn't been initialized/called previously.

protected void onCreate(Bundle b) {
    if(shouldCall()) { // I know if the method has been called before
        callMethodJustOnce();
    }
}

If you are looking to call this method only once ever, I would take a look at most answers in here recommending using Preferences. But if you are talking about once per time the app is brought to life, this should be achieved in onCreate(), as this should only be called once the app is initialized and started.

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • 1
    "But if you are talking about once per time the app is brought to life, this should be achieved in onCreate(), as this should only be called once the app is initialized and started." - That's not quite true. onCreate can be called again, for example when the orientation changes the current activity will be destroyed and created again. – C0deAttack Sep 01 '11 at 15:47
  • I think we are talking about the same thing. Yes, `onCreate()` will be called on orientation change, but isn't `onDestroy()` called first? If that is the case, wouldn't the app be 'brought to life' when `onCreate()` is called again? – nicholas.hauschild Sep 01 '11 at 15:57
  • No because the second time you're not starting the app from launch, you're just updating the UI for the new orientation. – C0deAttack Sep 01 '11 at 16:05
0

Create a variable in shared preferences that counts app open times then if 0 you call the method Happy coding :D

PedroAGSantos
  • 2,336
  • 2
  • 17
  • 34