MOBILE APPLICATION
DEVELOPMENT
Lecture 5: Android Project
Structure and Lifecycle
Course Lecturer: Menard Phiri,
Dept of CSIT, MUST
LESSON OUTCOMES
Understanding Android project structure
Understanding Android lifecycle
2
ANDROID PROJECT STRUCTURE
3
ANDROID PROJECT STRUCTURE CONT.
4
INTRODUCTION TO ACTIVITIES
Android programs have no main() method
Every Android App starts in an Activity
An activity provides the window in which the
app draws its UI.
One activity implements one screen in an
app.
At least one activity must be the main
5
activity
INTRODUCTION TO ACTIVITIES CONT.
Any activity can call another activity to
perform different actions.
Each activity must be register information
about them in the app’s manifest file.
Each activity must at least have an
onCreate() method that specifies the
behavior of the app
6
THE ACTIVITY LIFECYCLE
An Android app uses states to determine
what to do.
An activity transitions between seven stages
of lifecycle
The parent Activity class provides a core set
of seven callbacks that should be overridden
to drive the expect behavior:
–onCreate()
–onStart()
–onResume()
–onPause()
–onStop()
–onRestart() 7
–onDestroy()
THE ONCREATE() METHOD
This is invoked when the system first creates an
activity.
As such, this is must implement basic application
startup logic for the activity.
This method receives the parameter a Bundle
object, called savedInstanceState, containing the
activity's previously saved state.
If the activity has never existed before, the value
of the Bundle object is null.
8
THE ONSTART() METHOD
This is invoked when the activity enters the
Started state
It makes the activity visible to the user.
It prepares the activity to enter the foreground,
become interactive and completes very quickly.
Once it finishes, the activity enters the
Resumed state
9
THE ONRESUME() METHOD
This is invoked when the activity enters the
Resumed state and it is visible on the
foreground.
This is the state in which the app interacts
with the user.
The app stays in this state until something
happens to take focus away from the app.
10
THE ONPAUSE() METHOD
The system calls this method as the first
indication that the user is leaving your
activity
This event does not imply that the activity is
being destroyed
It indicates that the activity is no longer in
the foreground.
11
THE ONSTOP() METHOD
This is invoked when the activity is no longer
visible to the user.
This may happen because the activity is
being destroyed or a new activity is starting.
In all of these cases, the stopped activity is
no longer visible at all.
12
THE ONRESTART() METHOD
The system invokes this callback when an
activity in the Stopped state is about to
restart.
The onRestart() restores the state of the
activity from the time that it was stopped.
This callback is always followed by onStart().
13
THE ONDESTROY() METHOD
The system invokes this callback before an
activity is destroyed.
This callback is the final one that the activity
receives.
It is usually implemented to release activity's
resources when the activity is destroyed.
14