Lecture10 - Activity Life Cycle
Lecture10 - Activity Life Cycle
1. Oncreate() method
• It is the first method to be called when activity is first created (first
starts up)
• An activity does all its initial setup of "global" state in onCreate().
• It can be used to perform one-time initialization such as creating the
user interface.
• This is where normal static set up is done e.g create views, bind data to
lists, and so on.
• All activities must implement onCreate( ) method to do the initial setup
when the object is first instantiated.
• This method is passed a Bundle object containing the activity's previous
state, if that state was captured. i.e. onCreate(Bundle)
• Oncreate() is always followed by onStart()
Activity life cycle methods
2. onStart() Method
• This method is called just before the activity becomes
visible to the user or displayed to the user.
• The method is executed when the activity start running.
• It is followed by onResume() if the activity comes to the
foreground, or onStop() if it becomes hidden.
3. onResume() Method:
• This method is called when an activity result or a new intent
is delivered.
• At this point the activity is at the top of the activity stack,
with user input going to it.
• It is a good place to start animations and music.
• This method is always followed by onPause().
Activity life cycle methods
4. onPause() Method:
• This method is called when the device goes to sleep or when a new
activity is started.
• It runs when the activity is about to go into the background,
usually because another activity has been launched in front of it.
• The method is typically used to commit unsaved changes to
• persistent data, stop animations and other things that may be
• consuming CPU.
• onPause() Method is followed either by onResume() if the activity
returns back to the front, or by onStop() if it becomes invisible to
the user.
• The activity in this state is killable (destroyable) by the system
Activity life cycle methods
5. onStop() method:
• This method is called when the activity is no longer visible to the
user and it won’t be needed for a while.
• This may happen because it is being destroyed, or because
• another activity (either an existing one or a new one) has been
resumed and is covering it.
• The method is followed either by onRestart() if the activity is
coming back to interact with the user, or by onDestroy() if this
activity is going away.
• The activity in this state is killable (Destroyable) by the system.
• If memory is tight, onStop( ) may never be called (the system
may simply terminate the current process).
Activity life cycle methods
6. onRestart() Method:
• This method is called after the activity has been stopped, just
before being started again.
• If this method is called, it indicates that the activity is being
redisplayed to the user from a stopped state
• onRestart() method is always followed by onStart()
Activity life cycle methods
7. onDestroy() Method:
• This method is called just before the activity is destroyed (killed).
• It is the final call that the activity will receive.
• It could be called either because the activity is finishing or
because the system is temporarily destroying this instance of the
activity to save space.
• The activity in this state is killable (Destroyable) by the system.
• If memory is tight, onDestroy( ) may never be called (the system
may simply terminate your process).
Other activity methods
• onSaveInstanceState(Bundle):
• This method is called to allow the activity to save per-instance
state, such as a cursor position within a text field.
• Usually you won’t need to override it because the default
implementation saves the state for all your user interface
controls automatically.
• onRestoreInstanceState(Bundle):
• This is called when the activity is being reinitialized from a state
previously saved by the onSave-InstanceState( ) method.
• The default implementation restores the state of your user
interface.
Visible Lifetime
1. Java compiler: converts java source code to java byte code (class
files).