Game Development Reference
In-Depth Information
The activity can be brought back to a running state from a paused or stopped state. Note
again that when an activity is resumed from a paused or stopped state, it is still the same Java
instance in memory, so all the state and member variables are the same as before the activity
was paused or stopped.
An activity has some protected methods that we can override to get information about
state changes:
ï?® Activity.onCreate() : This is called when our activity is started up for the
first time. Here, we set up all the UI components and hook into the input
system. This method will get called only once in the life cycle of our activity.
ï?® Activity.onRestart() : This is called when the activity is resumed from a
stopped state. It is preceded by a call to onStop() .
ï?® Activity.onStart() : This is called after onCreate() or when the activity is
resumed from a stopped state. In the latter case, it is preceded by a call to
onRestart() .
ï?® Activity.onResume() : This is called after onStart() or when the activity is
resumed from a paused state (for example, when the screen is unlocked).
ï?® Activity.onPause() : This is called when the activity enters the paused state.
It might be the last notification we receive, as the Android system might
decide to kill our application silently. We should save all states we want to
persist in this method!
ï?® Activity.onStop() : This is called when the activity enters the stopped state.
It is preceded by a call to onPause() . This means that an activity is stopped
before it is paused. As with onPause() , it might be the last notification we
get before the Android system silently kills the activity. We could also save
persistent state here. However, the system might decide not to call this
method and just kill the activity. As onPause() will always be called before
onStop() and before the activity is silently killed, we'd rather save all our
stuff in the onPause() method.
ï?® Activity.onDestroy() : This is called at the end of the activity life cycle
when the activity is irrevocably destroyed. It's the last time we can persist
any information we'd like to recover the next time our activity is created
anew. Note that this method actually might never be called if the activity was
destroyed silently after a call to onPause() or onStop() by the system.
Figure 4-3 illustrates the activity life cycle and the method call order.
 
Search WWH ::




Custom Search