Android lifecycle methods don't seem to work the way I need
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.
开发者_JAVA技巧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.
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().
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.
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
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
...
}
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.
精彩评论