Download - Inside the android_application_framework
![Page 1: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/1.jpg)
Inside the Android Application Framework
![Page 2: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/2.jpg)
IntroductionYour 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
![Page 3: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/3.jpg)
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.
Every Android component has a managed lifecycle
![Page 4: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/4.jpg)
Basics of an Android Application
Activities
Tasks
Processes
![Page 5: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/5.jpg)
Activities and TasksAn Activity is a “molecule”: a discrete chunk of functionality
A task is a collection of Activities
A “process” is a standard Linux process
![Page 6: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/6.jpg)
Activities and Tasks
APK PackageProcess
ContentProvider
Service
ActivityActivity
APK Package
Process
Activity
ContentProvider
Process
Service
Activity
![Page 7: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/7.jpg)
APK Package
Process
Activity
ContentProvider
Process
Service
Activities and Tasks
APK PackageProcess
ContentProvider
Service
Task
ActivityActivity Activity
![Page 8: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/8.jpg)
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
![Page 9: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/9.jpg)
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”
![Page 10: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/10.jpg)
Process BasicsAndroid process == Linux process
By default, 1 process per APK
By default, 1 thread per process
All* components interleave events into the main thread
* Most
![Page 11: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/11.jpg)
Process LifecycleA process is started for a given user ID when needed
Binding to a Service
Binding to a ContentProvider
Starting an Activity
Firing an IntentReceiver
Remains running until killed by the system
![Page 12: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/12.jpg)
More on Activities
Activity Lifecycle
Examples of Common Use Cases
![Page 13: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/13.jpg)
The Directed Cyclic Graph of LifeActivities 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
![Page 14: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/14.jpg)
Activity LifecycleThree general “phases”
Starting up
onCreate(): first method called during lifetime, with prior state
onStart()/onRestart(): signal that execution is beginning
onResume(): signals that a previous pause is being undone
![Page 15: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/15.jpg)
Activity LifecycleNormal execution
onFreeze(): save UI state (NOT intended to save persistent data)
onPause: signals loss of focus and possible impending shutdown
![Page 16: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/16.jpg)
Activity LifecycleShutting down
onStop()/onDestroy(): final shutdown and process termination
Not guaranteed to be called (and usually not, except on finish()...)
![Page 17: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/17.jpg)
Activity Lifecycle ExamplesStarting a Child Activity
Child Activity + Process Shutdown
Returning to the Home Screen
Calling finish() Explicitly
Displaying a Dialog Box
Semi-Transparent Windows
Device Sleep
![Page 18: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/18.jpg)
Example: Child Activity Launched
Call sequence:
onCreate()
onStart()
onResume()
onFreeze()
onPause()
onStop()
onRestart()
onStart(), onResume(), ...
This is the “classic” scenario.
![Page 19: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/19.jpg)
Example: Child Activity + Process Death
Call sequence:
onCreate() (empty state)
onStart()
onResume()
onFreeze()
onPause()
onStop() (maybe)
onDestroy() (maybe)
onCreate() (with state), ...
Like the basic case, but onCreate() is called again, with the state saved in onFreeze().
![Page 20: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/20.jpg)
Example: User Hits ’Home’Call sequence:
onCreate()
onStart()
onResume()
onFreeze()
onPause()
onStop() (maybe)
onDestroy() (maybe)
Identical to the basic case -- that is, the Home key is not a special case.
![Page 21: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/21.jpg)
Example: finish() CalledCall sequence:
onCreate()
onStart()
onResume()
onPause()
onStop()
onDestroy()
Because the Activity has been explicitly told to quit and is being removed from the task (and history stack), onFreeze() is not called, and onDestroy() is reached.
![Page 22: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/22.jpg)
Example: Dialog BoxCall sequence:
onCreate()
onStart()
onResume()Despite appearances, dialog boxes are Views, and not Activities, so they have no effect on the owning Activity’s lifecycle.
![Page 23: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/23.jpg)
Example: Transparent/Non-fullscreen Child
Call sequence:
onCreate()
onStart()
onResume()
onFreeze()
onPause()
onResume()
The new partial-screen window leaves a portion of the previous window visible, so onPause() is followed by onResume() (without onStop()) when the child closes.
![Page 24: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/24.jpg)
Example: Device Goes to SleepCall sequence:
onCreate()
onStart()
onResume()
onFreeze()
onPause()
onResume()
The device going to sleep is identical to a non-fullscreen Activity being launched on top.
![Page 25: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/25.jpg)
Threads on Android
Overview
Loopers
Multi-thread Considerations
![Page 26: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/26.jpg)
Threading OverviewEach process has one thread (by default)
Most components share the single thread
Services and ContentProviders sometimes do not
![Page 27: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/27.jpg)
Threads and LoopersEach 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.
Loopers cannot accommodate multi-threaded access
They are designed to play nicely with MessageHandlers
![Page 28: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/28.jpg)
Threads and LoopersAPK PackageProcess
ThreadLooper
Message Queue
ThreadExternal Service
Calls
IntentReceive
r
Activity
Activity
UI Events
System Events
Local Service
Call
![Page 29: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/29.jpg)
Threads and ViewsViews use Looper messages to fire events
Since Loopers are 1:1 with threads, the View tree is too
Threads you create cannot directly touch a View
But, you can create a new Looper for your own thread
![Page 30: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/30.jpg)
Threads in Other ContextsServices & ContentProviders sometimes run in their own threads
...but still in the same process
Components can create threads, but must handle thread-safety
![Page 31: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/31.jpg)
Service LifecycleStarted by some other Component
Either explicitly, or implicitly by binding to it
Explicitly-started Services run until explicitly shut down
(or killed by the system during a memory crunch)
Implicitly-started Services run til the last client unbinds
![Page 32: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/32.jpg)
More on Processes
Resource Management
Processes & Security
Controlling Processes
![Page 33: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/33.jpg)
Process Resource ManagementSpawned by the special “Zygote” process
Process + pre-warmed Dalvik VM == responsiveness
Process runs under user ID unique to system
Process + User ID == security
![Page 34: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/34.jpg)
Processes & SecurityEach application is given a unique user ID
No exceptions!
...except these: init, Zygote, and the main runtime
Each application has direct access only to its own data
Other apps’ resources are available only via defined, explicitly-exposed APIs
i.e. Issuing Intents, binding to Services or ContentProviders
![Page 35: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/35.jpg)
Inter-Process Communication
Why??
Process Transparency
Binder in 30 Seconds
IPC using Parcelables
IPC using Bundles
Android IDL
![Page 36: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/36.jpg)
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.
![Page 37: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/37.jpg)
Process TransparencyProcess 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.
![Page 38: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/38.jpg)
Kernel Process
IPC OverviewBinder
Bundle
IntentReceive
r
Activity
CustomObjects
Service
Parcel
Parcelable
![Page 39: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/39.jpg)
Binder in 30 SecondsAll IPC goes through “The Binder”
Binder is implemented as a kernel module + system lib
Supports sophisticated cross-process data transport
The framework APIs “know” how to use Binder
Generally two entry points: Bundles & Parcelables
![Page 40: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/40.jpg)
IPC - ParcelablesA Parcelable is a class which can marshal its state to something Binder can handle -- namely, a “Parcel”
Standard Java serialization has semantics Parcelables don’t need
Supporting full serialization would mean wasting CPU cycles
![Page 41: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/41.jpg)
IPC - BundlesBundles are typesafe containers of primitives
That is, C-like primitives: ints, strings, etc.
Simple data-passing APIs use Bundles
Think of onFreeze() as passing data to your future self
Flat structure permits optimizations like memory-mapping
![Page 42: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/42.jpg)
IPC - AIDL“Android Interface Definition Language”
Used to build developer-friendly APIs using Parcelables
Preferred way to expose structured, complex-typed APIs
Compromise between efficiency and Java usability
![Page 43: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/43.jpg)
Wrapping Up
APKs are loose collections of components
Tasks (AKA apps) are bags of component instances that span processes & APKs
Managed lifecycles & IPC join the “seams”
![Page 44: Inside the android_application_framework](https://reader035.vdocuments.us/reader035/viewer/2022081400/55580161d8b42ae31c8b4eb7/html5/thumbnails/44.jpg)
Questions?