Android Application Model
Android Application Model
.apk
2
Application Components
Android applications do not have a single entry point (e.g. no
main() function).
They have essential components that the system can instantiate
and run as needed.
Four basic components
Components Description
Activity UI component typically corresponding to one screen
Service Background process without UI
Broadcast Receiver Component that responds to broadcast Intents
Content Provider Component that enables applications to share data
3
Components - Activity
An activity is usually a single screen:
— Implemented as a single class extending Activity.
— Displays user interface controls (views).
— Reacts on user input/events.
An application typically consists of several screens:
— Each screen is implemented by one activity.
— Moving to the next screen means starting a new activity.
— An activity may return a result to the previous activity.
4
Components - Activity (Cont)
Typically, one of the activities is marked as the first one that
should be presented to the user when the application is launched.
Created “Activity” must be defined into the application’s manifest.
5
Components - Service
A service does not have a visual user interface, but rather runs in
the background for an indefinite period time.
Example: music player, network download, etc
Each service extends the Service base class.
It is possible to bind to a running service and start the service if
it's not already running.
While connected, it is possible communicate with the service
through an interface defined in an AIDL (Android Interface
Definition Language).
Notification
Communication Service
Pause/rewind Background running
/stop/restart for playback
Media Player
Activity Binder
6
Components - Service (Cont)
Adding a "Service" with Android is quite similar than for an
"Activity".
7
Components - Broadcast Receivers
A broadcast receiver is a component that receives and reacts to
broadcast announcements (Intents).
Many broadcasts originate in system code.
E.g. announcements that the time zone has changed, that
the battery is low, etc.
SMS
8
Components - Broadcast Receivers
(Cont)
A broadcast receiver is a component that receives and reacts to
broadcast announcements. (Cont)
Applications can also initiate broadcasts.
E.g. to let other applications know that some data has been
downloaded to the device and is available for them to use.
9
Components - Content Providers
Application
Activity
Activity Activity
Activity
Application Application
Activity
Activity Content
Content Resolver
Resolver Service
Service
Content
Content Resolver
Resolver Content
Content Provider
Provider Content
Content Resolver
Resolver
Remote
Data
Data SQLite XML
XML Store
startActivity(new
Intent(Intent.EDIT_ACTION,Uri.parse("content://contacts/people/1"));
12
Intents (Cont)
Intent Filters
A component's intent filters in the manifest file inform Android
of the kinds of intents the component is able to handle.
An example
13
Intents (Cont)
Intent Filters (Cont)
An example (Cont)
① A component can have any number of intent filters, each
one declaring a different set of capabilities.
② The first filter in the example indicates that the
activity is the entry point for the application.
③ The second filter declares an action that the activity
can perform on a particular type of data.
14
Android Component Model
An Android application is packaged in a .apk file.
A .apk file is a collection of components.
Application
(.apk)
Process
Activity
Activity Activity
Activity
Activity
Activity Activity
Activity
Content
Content Provider
Provider
Service
Service Service
Service
Activity
Activity Asynchronous Message (Intent) Activity
Activity
Context.startActivity(Intent) No return
or
Activity.startActivityForResult
(Intent, Request_Code)
To get some result
(e.g. to get a photo)
16
Activities and Tasks (Cont)
Tasks
A task is a collection of related Activities.
It is capable of spanning multiple processes.
Process Process
Activity
Activity Activity
Activity Activity
Activity Activity
Activity
Activity
Activity Activity
Activity Activity
Activity Activity
Activity
Content
Content Provider
Provider Content
Content Provider
Provider
Service
Service Service
Service Service
Service Service
Service
17
Activities and Tasks (Cont)
Tasks (Cont)
All activities in a task are arranged in a stack.
Instance
Instance of
of Activity
Activity B
B The one that's currently
running
Instance
Instance of
of Activity
Activity C
C
Instance
Instance of
of Activity
Activity B
B
Instance
Instance of
of Activity
Activity A
A The one that began the task
(typically, an activity the user
A Stack selected in the application
launcher)
19
Activities and Tasks (Cont)
Affinities (Cont)
Two circumstances where the affinity comes into play:
① FLAG_ACTIVITY_NEW_TASK flag
If the Intent object passed to startActivity() contains the
FLAG_ACTIVITY_NEW_TASK flag, the system looks for a
different task to house the new activity.
– If there's already an existing task with the same affinity as
the new activity, the activity is launched into that task.
– If not, it begins a new task.
② allowTaskReparenting attribute
If an activity has its allowTaskReparenting attribute set to
"true", it can move from the task it starts in to the task it
has an affinity for when that task comes to the fore.
20
Activities and Tasks (Cont)
Launch Modes
There are four launch modes:
standard (default) / singleTop / singleTask / singleInstance
21
Activities and Tasks (Cont)
Launch Modes (Cont)
The modes differ from each other on four points:
① Which task will hold the activity that responds to the
intent
Original Task Original Task New Task
New
New Activity
Activity
Activity
Activity A
A Activity
Activity A
A
Root
Root Activity
Activity Root
Root Activity
Activity New
New Activity
Activity
standard/singleTop
without singleTask/singleInstance
FLAG_ACTIVITY_NEW_TASK
22
Activities and Tasks (Cont)
Launch Modes (Cont)
The modes differ from each other on four points: (Cont)
② Whether there can be multiple instances of the activity
Task A Task B Task A Task B
Activity
Activity B
B
Activity
Activity C
C
Activity
Activity B
B Activity
Activity C
C Activity
Activity B
B
Activity
Activity A
A Activity
Activity D
D Activity
Activity A
A Activity
Activity C
C
24
Activities and Tasks (Cont)
Launch Modes (Cont)
The modes differ from each other on four points: (Cont)
④ Whether a new instance of the class will be launched to
handle a new intent
Activity
Activity D
D
Activity
Activity D
D Activity
Activity D
D Activity
Activity D
D The existing
An intent arrives for instance D is
an activity of type D expected to handle
Activity
Activity C
C Activity
Activity C
C Activity
Activity C
C the new intent
(since it's at the top
Activity
Activity B
B Activity
Activity B
B Activity
Activity B
B of the stack)
Activity
Activity A
A Activity
Activity A
A Activity
Activity A
A
25
Activities and Tasks (Cont)
Launch Modes (Cont)
The modes differ from each other on four points: (Cont)
④ Whether a new instance of the class will be launched to
handle a new intent (Cont)
Activity
Activity B
B Activity
Activity B
B
Activity
Activity D
D Activity
Activity D
D Activity
Activity D
D The existing
An intent arrives for instance B is not
an activity of type B expected to handle
Activity
Activity C
C Activity
Activity C
C Activity
Activity C
C the new intent
(since it's not at the
Activity
Activity B
B Activity
Activity B
B Activity
Activity B
B top of the stack)
Activity
Activity A
A Activity
Activity A
A Activity
Activity A
A
26
Activities and Tasks (Cont)
Launch Modes (Cont)
The modes differ from each other on four points: (Cont)
④ Whether a new instance of the class will be launched to
handle a new intent (Cont)
A "singleInstance"
activity is always at
An intent arrives for the top of the stack,
Activity
Activity B
B
an activity of type B Activity
Activity B
B so it is always in
position to handle
the intent.
Original Task If B is"singleInstance"
27
Activities and Tasks (Cont)
Launch Modes (Cont)
The modes differ from each other on four points: (Cont)
④ Whether a new instance of the class will be launched to
handle a new intent (Cont)
Activity
Activity B
B Activity
Activity B
B Activity B can handle
An intent arrives for the intent since it is
Activity
Activity A
A
an activity of type B Activity
Activity A
A in position.
Activity
Activity A
A Activity
Activity A
A Activity B cannot
handle the intent
An intent arrives for since it is not in
Activity
Activity B
B an activity of type B Activity
Activity B
B position and the
intent is dropped.
28
Activities and Tasks (Cont)
Clearing the Stack
Default Control
If the user leaves a task for a long time, the system
clears the task of all activities except the root activity.
Some activity attributes that can be used to modify the
default control:
① If alwaysRetainTaskState is set to the root activity of a
task
– The task retains all activities in its stack even after a long period.
② If clearTaskOnLaunch is set to the root activity of a task
– The stack is cleared down to the root activity whenever the user
leaves the task and returns to it.
– The user always returns to the task in its initial state, even after a
momentary absence.
29
Activities and Tasks (Cont)
Clearing the Stack (Cont)
Some activity attributes that can be used to modify the
default control: (Cont)
③ If finishOnTaskLaunch is set to an activity of a task
- The activity remains part of the task only for the current session.
- If the user leaves and then returns to the task, it no longer is
present.
31
Processes and Threads
Processes
When the first of an application's components needs to be
run, Android starts a Linux process for it with a single
thread of execution (Main Thread).
Application
Application 1 1
Process
Process Main
Main Thread
Thread
(.apk)
(.apk)
32
Processes and Threads (Cont)
Process (Cont)
We can specify a process where an individual component
should run by setting a process name to “process” attribute
of <activity>, <service>, <receiver>, or <provider>.
Each component can run in its own process.
Some components share a process while others do not.
Components of different applications also can run in the
same process.
We can set a default value that applies to all components by
setting a default process to "process attribute of
<application>.
33
Processes and Threads (Cont)
Threads
Main Thread
All components are instantiated in the main thread of the
specified process.
System calls to the components are dispatched from the
main thread.
Methods that respond to those calls always run in the
main thread of the process.
No component should perform long or blocking
operations (e.g. network downloads, computation loops)
34
Processes and Threads (Cont)
Threads (Cont)
Anything that may not be completed quickly should be
assigned to a different thread.
Threads are created in code using standard Java Thread
objects.
Some convenience classes Android provides for managing
threads:
Looper for running a message loop within a thread
Handler for processing messages
HandlerThread for providing a handy way for starting a
new thread that has a looper
35
Component Lifecycles
Activity Lifecycle
Three states
State Description
Running • An activity is in the foreground of the screen (at the top of
the activity stack for the current task).
Paused • An activity has lost focus but is still visible to the user.
Stopped • An activity is completely obscured by another activity.
• It still retains all state and member information.
36
Component Lifecycles
Activity Lifecycle (Cont)
An activity's overall lifecycle
onCreate()
Called when the activity is
first created or when the
activity was killed
onStart()
Called just before the
activity becomes visible to
user
onRestart()
Called after the activity has
been stopped, just prior to
it being started again
37
Component Lifecycles (Cont)
Activity Lifecycle (Cont)
An activity's overall lifecycle (Cont)
onResume()
Called just before the activity starts interacting with the user
At this point, the activity is at the top of the activity stack, with
user input going to it.
onPause()
Called when the system is about to start resuming another activity
This method is typically used to commit unsaved changes to
persistent data, stop animations and other things that may be
consuming CPU, and so on.
38
Component Lifecycles (Cont)
Activity Lifecycle (Cont)
An activity's overall lifecycle (Cont)
onStop()
Called when the activity is no longer visible to the user
This may happen because it is being destroyed, or because another
activity has been resumed and is covering it.
onDestroy()
Called before the activity is destroyed
39
Component Lifecycles (Cont)
Activity Lifecycle (Cont)
Three nested loops for the entire lifecycle
Visible Lifetime
• During this time, the user can see the activity on-screen, though it
may be in the foreground and interacting with the user.
• onStart() and onStop() can be called multiple times, as the activity
alternates between being visible and hidden to the user.
Foreground Lifetime
• During this time, the activity is in front of all other activities on
screen and is interacting with the user.
40
Component Lifecycles (Cont)
Service Lifecycle
Two ways that a service can be used
The service can be started and allowed to run until
someone stops it or it stops itself.
– started by calling Context.startService() and stopped by calling
Context.stopService()
The service can be operated programmatically using an
interface that it defines and exports.
– Clients establish a connection to the Service object and use that
connection to call into the service.
– established by calling Context.bindService() and closed by calling
Context.unbindService()
41
Component Lifecycles (Cont)
Service Lifecycle (Cont)
42
Component Lifecycles (Cont)
Broadcast Receiver Lifecycle
Only single callback method
43
Component Lifecycles (Cont)
Processes and Lifecycles
Android tries to maintain a process for as long as possible,
but eventually it will need to remove old processes when
memory runs low.
To determine candidates to be killed, Android places each
process into an "importance hierarchy" based on the
components running in it and the state of those components.
44
Component Lifecycles (Cont)
Processes and Lifecycles (Cont)
Five levels in the Importance Hierarchy
45