Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

UNDERSTANDING

ACTIVITIES
Delving into Android
Activity Lifecycle
An application typically exists in one
of four main states:
❖ Launched – activity launched
❖ Running – activity running
❖ Killed – application process
killed
❖ Shutdown – activity shutdown
Understanding Activity
Lifecycle Callback
Methods
• onCreate( )
✔ Load into the content view all of the
layouts you plan to use, so that
Android can create the UI prior to
displaying it when the onStart( )
method is called.
✔ Initialize any activity-level variables
you have defined within the activity
class definition.
Understanding Activity Lifecycle
Callback Methods (cont.)
• onStart( )
✔ The onStart() method is also called
on the path following onRestart() in
the preceding lifecycle model.
✔ Even if you are tempted to, you
need to be aware that if your
activity is following the onRestart()
path, instance state is still not fully
restored at this point and won’t be
until the subsequent call to
onRestoreInstanceState() is made.
Understanding Activity Lifecycle
Callback Methods (cont.)
onResume( )
✔ The only logic you should normally
consider adding to onResume() is
related to the real-time visual
aspects changing your activity,
including
1. Animation
2. Video
3. Custom view transition effects
onPause( )

Understanding
Activity Lifecycle onStop( )
Callback Methods
(cont.)
onDestroy( )
Appreciating the Objectives
of the Activity Lifecycle
As you start composing applications
with ever greater numbers of
activities, it will serve you well to
keep the following principles in mind:
• Activities should have a focus.
• Save state, and then save it
again.
• Give every view an ID
• Applications and activities can
disappear
Working with the Default
Approach to Configuration
Change
The following callbacks cover the
flow that allows you to save and
restore state and resources across a
configuration change:
•onSaveInstanceState()
•onRestoreInstanceState()

You might also like