Inside The Android Application Framework
Inside The Android Application Framework
Inside The Android Application Framework
Introduction
Your host: Dan Morrill, Developer Advocate
Android is a complete OS, not just a framework
Even the friendliest abstraction still has “seams”
Let’s demystify Android’s seams
Managed Component Lifecycles
An Android APK is a collection of components
Components share a set of resources
Databases, preferences, file space, etc.
Also: a Linux process.
Activities
Tasks
Processes
Activities and Tasks
An Activity is a “molecule”: a discrete chunk of functionality
A task is a collection of Activities
A “process” is a standard Linux process
Activities and Tasks
ContentProvider
ContentProvider
Process
Service Service
Process Process
APK Package APK Package
Activities and Tasks
Task ContentProvider
ContentProvider
Process
Service Service
Process Process
APK Package APK Package
Activities Are...
...a concrete class in the API
...an encapsulation of a particular operation
...run in the process of the .APK which installed them
...optionally associated with a window (UI)
...an execution Context
Tasks Are...
...more of a notion than a concrete API entity
...a collection of related Activities
...capable of spanning multiple processes
...associated with their own UI history stack
...what users on other platforms know as “applications”
Process Basics
Android process == Linux process
By default, 1 process per APK
By default, 1 thread per process
All* components interleave events into the main thread
*
Most
Process Lifecycle
A process is started for a given user ID when needed
Binding to a Service
Binding to a ContentProvider
Starting an Activity
Firing an IntentReceiver
Activity Lifecycle
Examples of Common Use
Cases
The Directed Cyclic Graph of Life
Activities have several states
Lifecycle methods are called on
transitions
You typically don’t need to use
them all, but they are there
http://code.google.
com/android/reference/android/app/
Activity.html
Activity Lifecycle
Three general “phases”
Starting up
Call sequence:
onCreate()
onStart()
The new partial-screen
window leaves a portion of
onResume() the previous window visible,
onFreeze() so onPause() is followed by
onPause()
onResume() (without
onStop()) when the child
onResume() closes.
Example: Device Goes to Sleep
Call sequence:
onCreate()
onStart()
onResume() The device going to sleep is
identical to a non-fullscreen
onFreeze()
Activity being launched on
onPause() top.
onResume()
Threads on Android
Overview
Loopers
Multi-thread Considerations
Threading Overview
Each process has one thread (by default)
Most components share the single thread
Services and ContentProviders sometimes do not
Threads and Loopers
Each thread has a Looper to handle a message queue
Events from all components are interleaved into Looper
e.g. View UI events, IntentReceivers firing, etc.
Resource Management
Processes & Security
Controlling Processes
Process Resource Management
Spawned by the special “Zygote” process
Process + pre-warmed Dalvik VM == responsiveness
Why??
Process Transparency
Binder in 30 Seconds
IPC using Parcelables
IPC using Bundles
Android IDL
Why??
All this process/Activity/task stuff is confusing... why?
It’s all for the noble goal of efficiency (i.e. speed.)
Serialization is slooow; memory transfers are slooow.
CPU is not the bottleneck: think memory & bandwidth.
Process Transparency
Process management is transparent to code.
...almost. In some cases, it’s unavoidably visible.
Lifecycle is seamless, but data sometimes isn’t.
Specific APIs send data across process boundaries.
IPC Overview
Kernel Process
Binder
Parcel
Parcelable
Bundle Custom
Objects