adohertyd
adohertyd

Reputation: 2689

Android life cycle: When each step should happen

This may be a bit of a stupid question but when is the best part of the Android life cycle to implement each step? The flow of my game is as follows:

  1. Before onCreate: Data that is stored in a JSON file is parsed into String Lists when the game starts
  2. onCreate: Basic UI is generated
  3. onStart/onResume: Game starts: item selected at random from the lists, user selects corresponding item to proceed
  4. If the user is correct, another item is selected from the lists. Occurs 10 times
  5. After 10 items, game ends and score is displayed to user

Would this be considered good practice? I'm a bit confused about the life cycle steps

Upvotes: 0

Views: 116

Answers (2)

TronicZomB
TronicZomB

Reputation: 8747

This might help with understanding the lifecycle of an Android app more. The following is quoted from that site:

As mentioned in the previous section, the lifecycle of an activity has 4 states and 3 lifetime periods. If you want to monitor and adding your own code logics to an activity, you can use the following 7 basic callback methods provided by the android.app.Activity class:

  • onCreate() - Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one. onCreate() is always followed by onStart().

  • onRestart() - Called after your activity has been stopped and prior to it being started again. onRestart() is always followed by onStart().

  • onStart() - Called when the activity is becoming visible to the user. onStart() is followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

  • onResume() - Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it. onResume() is always followed by onPause().

  • onPause() - Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns. onPause() is followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user.

  • onStop() - Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed. onStop() is followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away.

  • onDestroy() - The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

Upvotes: 1

Artem Zinnatullin
Artem Zinnatullin

Reputation: 4447

How are you pre-loading data before onCreate? You got no Context object to work with app filesystem before your onCreate called. Here is documentation for Activity lifecycle, as you can see, onCreate is the first method where you can run your code.

I suggest to put data loading in other thread, for example AsyncTask.

So:

  1. Generate basic UI
  2. Start data pre-loading NOT IN MAIN THREAD
  3. Update UI after data loaded

Upvotes: 0

Related Questions