0

Coming to Android from an iOS background, I'm finding that the lifecycle methods in Android programming are not set up in the way I expect.

For example, I need to perform a task once each time the user starts using my app. The app consists of a main activity which spawns several child activities as it runs.

If I put the task in the onCreate() method of the main activity, it definitely gets called the first time the user starts the app. However, it doesn't always get called if the user exits the app and re-enters it later. I presume that this is because Android hasn't really destroyed the Activity and as such it does not need to re-create it.

If I put the task in onStart() or onResume() instead, then it gets called whenever the user launches the app, but it also gets called whenever they return from one of the child activities as well, which is not what I want.

Where can I put my task such that it only executes once each time the user starts the app?

Thanks.

Frank LaRosa
  • 3,533
  • 6
  • 26
  • 32

5 Answers5

1

Define a global boolean for your Main Activity, 'showSplash' for example, and initialize it as "true". Then, when your 'onCreate' method is first called, you set it to "false". Then, anytime the 'onCreate' method is called, you check if the boolean is "false". If it is, don't show the splash or anything you want to do, if not, show it or do what you want to do.

EDIT:

Not a good approach like said in comments before - call finish() in onPause().

evilone
  • 22,410
  • 7
  • 80
  • 107
  • How do I tell when the application is closing then? Seems like the same problem. – Frank LaRosa Apr 25 '11 at 16:16
  • the you need to force close the application, which is not quite good approach – evilone Apr 25 '11 at 16:17
  • I think what I need to do is subclass Application. In the documentation for the Application class it says, "You can provide your own implementation by specifying its name in your AndroidManifest.xml's tag, which will cause that class to be instantiated for you when the process for your application/package is created." However, in the documentation for the tag, I could find no attribute for specifying the name of the Application class. – Frank LaRosa Apr 25 '11 at 16:18
  • I need to do the task every time the user starts the application, not just the first time. – Frank LaRosa Apr 25 '11 at 16:19
0

I've been reading about it and found an onCreate() method in the Application class.

You can probably subclass application and override this method to implement what you need. I think this works like the Application Delegate class in Cocoa, but I'm not 100% certain.

Disclaimer: Haven't tried this yet.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Tejaswi Yerukalapudi
  • 8,987
  • 12
  • 60
  • 101
  • Just tried it. It doesn't work - onCreate() in Application is not called each time the user launches the app. Only the first time, or if the app happens to be destroyed and relaunched. – Frank LaRosa Apr 25 '11 at 16:51
0

An unpleasant way to do it is to check the calling/previous Activity - if it is not one of your own, call your initialization code. how to know the calling activity in android shows a good way of doing it. Use the putExtra(..) method on each of your own internal Activities to flag that they are your own.

Community
  • 1
  • 1
Haphazard
  • 10,900
  • 6
  • 43
  • 55
0

Haven't tried this but you can have a subclass from the android.app.Application class and implement the onCreate method. According to the docs

Called when the application is starting, before any other application objects have been created. Implementations should be as quick as poss
advantej
  • 20,155
  • 4
  • 34
  • 39
0

Frank... The way I do this is by writing a bundle in onSaveInstanceState or by writing a serializable object in onRetainNonConfigurationState. Then in pseudo code i do this in onCreate:

state= getLastNonConfigurationStateInstance or 
onCreate(Bundle state)

if (state == null) { // APP LAUNCHED FIRST TIME
... getPrefs
... if (prefs != null) get state from prefs
....getIntent
... if (intent != null) get state from getExtras
... check sharedPrefs for EULA_ACCEPTED_FLAG
// SUPPORT EULA
            SharedPreferences eulaPrefs = getSharedPreferences(PREFERENCES_EULA, MODE_PRIVATE); // singleton
            if (eulaPrefs == null || (eulaPrefs != null && eulaPrefs.getBoolean(PREFERENCE_EULA_ACCEPTED, false) == false)) {
                showDialog(DIALOG_EULA_ACCEPT);  
            }
}
else { // APP LAUNCHED AT LEAST SECOND TIME
...
}
JAL
  • 3,319
  • 2
  • 20
  • 17