0
Inside the Android Application Framework
IntroductionYour host: Dan Morrill, Developer AdvocateAndroid is a complete OS, not just a frameworkEven the friendliest a...
Managed Component Lifecycles An Android APK is a collection of components Components share a set of resources    Databases...
Basics of an Android Application Activities Tasks Processes
Activities and TasksAn Activity is a “molecule”: a discrete chunk of functionalityA task is a collection of ActivitiesA “p...
Activities and Tasks              Activity   Activity   Activity   Activity                          ContentProvider    Co...
Activities and Tasks   Activity   Activity     Activity   Activity                    Task    ContentProvider    ContentPr...
Activities Are......a concrete class in the API...an encapsulation of a particular operation...run in the process of the ....
Tasks Are......more of a notion than a concrete API entity...a collection of related Activities...capable of spanning mult...
Process BasicsAndroid process == Linux processBy default, 1 process per APKBy default, 1 thread per processAll* components...
Process LifecycleA process is started for a given user ID when needed   Binding to a Service   Binding to a ContentProvide...
More on Activities 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...
Activity LifecycleThree general “phases”Starting up    onCreate(): first method called during lifetime, with prior state  ...
Activity LifecycleNormal execution    onFreeze(): save UI state (NOT intended to save persistent data)    onPause: signals...
Activity LifecycleShutting down    onStop()/onDestroy(): final shutdown and process termination    Not guaranteed to be ca...
Activity Lifecycle ExamplesStarting a Child ActivityChild Activity + Process ShutdownReturning to the Home ScreenCalling f...
Example: Child Activity Launched Call sequence:    onCreate()    onStart()    onResume()    onFreeze()                   T...
Example: Child Activity + ProcessDeath Call sequence:    onCreate() (empty state)    onStart()    onResume()              ...
Example: User Hits ’Home’Call sequence:   onCreate()   onStart()   onResume()            Identical to the basic case   onF...
Example: finish() CalledCall sequence:   onCreate()   onStart()     Because the Activity has   onResume()    been explicit...
Example: Dialog BoxCall sequence:   onCreate()   onStart()                 Despite appearances,   onResume()    dialog box...
Example: Transparent/Non-fullscreen Child Call sequence:    onCreate()    onStart()                         The new partia...
Example: Device Goes to Sleep Call sequence:    onCreate()    onStart()    onResume()    The device going to sleep is     ...
Threads on Android Overview Loopers Multi-thread Considerations
Threading OverviewEach process has one thread (by default)Most components share the single threadServices and ContentProvi...
Threads and LoopersEach thread has a Looper to handle a message queueEvents from all components are interleaved into Loope...
Threads and Loopers                    APK Package                      Process              Thread               Local   ...
Threads and ViewsViews use Looper messages to fire eventsSince Loopers are 1:1 with threads, the View tree is tooThreads y...
Threads in Other ContextsServices & ContentProviders sometimes run in their own threads   ...but still in the same process...
Service LifecycleStarted by some other Component   Either explicitly, or implicitly by binding to itExplicitly-started Ser...
More on Processes Resource Management Processes & Security Controlling Processes
Process Resource ManagementSpawned by the special “Zygote” process   Process + pre-warmed Dalvik VM == responsivenessProce...
Processes & SecurityEach application is given a unique user ID   No exceptions!   ...except these: init, Zygote, and the m...
Inter-Process Communication Why?? Process Transparency Binder in 30 Seconds IPC using Parcelables IPC using Bundles Androi...
Why??All this process/Activity/task stuff is confusing... why?It’s all for the noble goal of efficiency (i.e. speed.)Seria...
Process TransparencyProcess management is transparent to code....almost. In some cases, it’s unavoidably visible.Lifecycle...
IPC Overview Kernel                                                 Process          Binder                               ...
Binder in 30 SecondsAll IPC goes through “The Binder”   Binder is implemented as a kernel module + system lib   Supports s...
IPC - ParcelablesA Parcelable is a class which can marshal its state to somethingBinder can handle -- namely, a “Parcel”St...
IPC - BundlesBundles are typesafe containers of primitives   That is, C-like primitives: ints, strings, etc.Simple data-pa...
IPC - AIDL“Android Interface Definition Language”Used to build developer-friendly APIs using ParcelablesPreferred way to e...
Wrapping Up APKs are loose collections of components Tasks (AKA apps) are bags of component instances that span processes ...
Questions?
Upcoming SlideShare
Loading in...5
×

Inside the android_application_framework

479

Published on

Published in: Education, Business, Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
479
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
30
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Inside the android_application_framework"

  1. 1. Inside the Android Application Framework
  2. 2. IntroductionYour host: Dan Morrill, Developer AdvocateAndroid is a complete OS, not just a frameworkEven the friendliest abstraction still has “seams”Let’s demystify Android’s seams
  3. 3. 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
  4. 4. Basics of an Android Application Activities Tasks Processes
  5. 5. Activities and TasksAn Activity is a “molecule”: a discrete chunk of functionalityA task is a collection of ActivitiesA “process” is a standard Linux process
  6. 6. Activities and Tasks Activity Activity Activity Activity ContentProvider ContentProvider Process Service Service Process Process APK Package APK Package
  7. 7. Activities and Tasks Activity Activity Activity Activity Task ContentProvider ContentProvider Process Service Service Process Process APK Package APK Package
  8. 8. 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
  9. 9. 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”
  10. 10. Process BasicsAndroid process == Linux processBy default, 1 process per APKBy default, 1 thread per processAll* components interleave events into the main thread *Most
  11. 11. 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 IntentReceiverRemains running until killed by the system
  12. 12. More on Activities Activity Lifecycle Examples of Common Use Cases
  13. 13. 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
  14. 14. 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
  15. 15. Activity LifecycleNormal execution onFreeze(): save UI state (NOT intended to save persistent data) onPause: signals loss of focus and possible impending shutdown
  16. 16. Activity LifecycleShutting down onStop()/onDestroy(): final shutdown and process termination Not guaranteed to be called (and usually not, except on finish()...)
  17. 17. Activity Lifecycle ExamplesStarting a Child ActivityChild Activity + Process ShutdownReturning to the Home ScreenCalling finish() ExplicitlyDisplaying a Dialog BoxSemi-Transparent WindowsDevice Sleep
  18. 18. Example: Child Activity Launched Call sequence: onCreate() onStart() onResume() onFreeze() This is the “classic” onPause() scenario. onStop() onRestart() onStart(), onResume(), ...
  19. 19. Example: Child Activity + ProcessDeath Call sequence: onCreate() (empty state) onStart() onResume() Like the basic case, but onCreate() is called again, onFreeze() with the state saved in onPause() onFreeze(). onStop() (maybe) onDestroy() (maybe) onCreate() (with state), ...
  20. 20. Example: User Hits ’Home’Call sequence: onCreate() onStart() onResume() Identical to the basic case onFreeze() -- that is, the Home key is onPause() not a special case. onStop() (maybe) onDestroy() (maybe)
  21. 21. Example: finish() CalledCall sequence: onCreate() onStart() Because the Activity has onResume() been explicitly told to quit and is being removed from onPause() the task (and history stack), onStop() onFreeze() is not called, onDestroy() and onDestroy() is reached.
  22. 22. Example: Dialog BoxCall sequence: onCreate() onStart() Despite appearances, onResume() dialog boxes are Views, and not Activities, so they have no effect on the owning Activity’s lifecycle.
  23. 23. Example: Transparent/Non-fullscreen Child 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.
  24. 24. 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()
  25. 25. Threads on Android Overview Loopers Multi-thread Considerations
  26. 26. Threading OverviewEach process has one thread (by default)Most components share the single threadServices and ContentProviders sometimes do not
  27. 27. Threads and LoopersEach thread has a Looper to handle a message queueEvents 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
  28. 28. Threads and Loopers APK Package Process Thread Local Intent Looper Service Receive Call r UI Activity Events Thread External Message Service Activity Queue System Calls Events
  29. 29. Threads and ViewsViews use Looper messages to fire eventsSince Loopers are 1:1 with threads, the View tree is tooThreads you create cannot directly touch a ViewBut, you can create a new Looper for your own thread
  30. 30. Threads in Other ContextsServices & ContentProviders sometimes run in their own threads ...but still in the same processComponents can create threads, but must handle thread-safety
  31. 31. Service LifecycleStarted by some other Component Either explicitly, or implicitly by binding to itExplicitly-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
  32. 32. More on Processes Resource Management Processes & Security Controlling Processes
  33. 33. Process Resource ManagementSpawned by the special “Zygote” process Process + pre-warmed Dalvik VM == responsivenessProcess runs under user ID unique to system Process + User ID == security
  34. 34. Processes & SecurityEach application is given a unique user ID No exceptions! ...except these: init, Zygote, and the main runtimeEach application has direct access only to its own dataOther apps’ resources are available only via defined,explicitly-exposed APIs i.e. Issuing Intents, binding to Services or ContentProviders
  35. 35. Inter-Process Communication Why?? Process Transparency Binder in 30 Seconds IPC using Parcelables IPC using Bundles Android IDL
  36. 36. 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.
  37. 37. 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.
  38. 38. IPC Overview Kernel Process Binder Parcel Parcelable Bundle Custom Objects Intent Activity Service Receive r
  39. 39. Binder in 30 SecondsAll IPC goes through “The Binder” Binder is implemented as a kernel module + system lib Supports sophisticated cross-process data transportThe framework APIs “know” how to use BinderGenerally two entry points: Bundles & Parcelables
  40. 40. IPC - ParcelablesA Parcelable is a class which can marshal its state to somethingBinder can handle -- namely, a “Parcel”Standard Java serialization has semantics Parcelables don’t need Supporting full serialization would mean wasting CPU cycles
  41. 41. 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 selfFlat structure permits optimizations like memory-mapping
  42. 42. IPC - AIDL“Android Interface Definition Language”Used to build developer-friendly APIs using ParcelablesPreferred way to expose structured, complex-typed APIsCompromise between efficiency and Java usability
  43. 43. 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”
  44. 44. Questions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×