Inside the Android Application Framework

download Inside the Android Application Framework

of 44

Transcript of Inside the Android Application Framework

  • 8/9/2019 Inside the Android Application Framework

    1/44

    Inside the Android Application Framework

  • 8/9/2019 Inside the Android Application Framework

    2/44

    IntroductionYour host: Dan Morrill, Developer Advocate

    Android is a complete OS, not just a framework

    Even the friendliest abstraction still has seams

    Lets demystify Androids seams

  • 8/9/2019 Inside the Android Application Framework

    3/44

    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

  • 8/9/2019 Inside the Android Application Framework

    4/44

    Basics of an Android Application

    Activities

    Tasks

    Processes

  • 8/9/2019 Inside the Android Application Framework

    5/44

    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

  • 8/9/2019 Inside the Android Application Framework

    6/44

    Activities and Tasks

    APK Package

    Process

    ContentProvider

    Service

    ActivityActivity

    APK Package

    Process

    Activity

    ContentProvider

    Process

    Service

    Activity

  • 8/9/2019 Inside the Android Application Framework

    7/44

    APK Package

    Process

    Activity

    ContentProvider

    Process

    Service

    Activities and Tasks

    APK Package

    Process

    ContentProvider

    Service

    Task

    ActivityActivity Activity

  • 8/9/2019 Inside the Android Application Framework

    8/44

    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

  • 8/9/2019 Inside the Android Application Framework

    9/44

    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

  • 8/9/2019 Inside the Android Application Framework

    10/44

    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

  • 8/9/2019 Inside the Android Application Framework

    11/44

    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

  • 8/9/2019 Inside the Android Application Framework

    12/44

    More on Activities

    Activity Lifecycle

    Examples of Common UseCases

  • 8/9/2019 Inside the Android Application Framework

    13/44

    The Directed Cyclic Graph of Life

    Activities have several states

    Lifecycle methods are called ontransitions

    You typically dont need to usethem all, but they are there

    http://code.google.com/android/reference/android/app/Activity.html

    http://code.google.com/android/reference/android/app/Activity.htmlhttp://code.google.com/android/reference/android/app/Activity.htmlhttp://code.google.com/android/reference/android/app/Activity.htmlhttp://code.google.com/android/reference/android/app/Activity.htmlhttp://code.google.com/android/reference/android/app/Activity.htmlhttp://code.google.com/android/reference/android/app/Activity.html
  • 8/9/2019 Inside the Android Application Framework

    14/44

    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

  • 8/9/2019 Inside the Android Application Framework

    15/44

    Activity LifecycleNormal execution

    onFreeze(): save UI state (NOT intended to save persistent data)

    onPause: signals loss of focus and possible impending shutdown

  • 8/9/2019 Inside the Android Application Framework

    16/44

    Activity LifecycleShutting down

    onStop()/onDestroy(): final shutdown and process termination

    Not guaranteed to be called (and usually not, except on finish()...)

  • 8/9/2019 Inside the Android Application Framework

    17/44

    Activity Lifecycle Examples

    Starting a Child Activity

    Child Activity + Process Shutdown

    Returning to the Home Screen

    Calling finish() Explicitly

    Displaying a Dialog Box

    Semi-Transparent Windows

    Device Sleep

  • 8/9/2019 Inside the Android Application Framework

    18/44

    Example: Child Activity Launched

    Call sequence:

    onCreate()

    onStart()

    onResume()

    onFreeze()

    onPause()

    onStop()

    onRestart()

    onStart(), onResume(), ...

    This is the classicscenario.

  • 8/9/2019 Inside the Android Application Framework

    19/44

    Example: Child Activity + ProcessDeath

    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 inonFreeze().

  • 8/9/2019 Inside the Android Application Framework

    20/44

    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 isnot a special case.

  • 8/9/2019 Inside the Android Application Framework

    21/44

    Example: finish() CalledCall sequence:

    onCreate()

    onStart()

    onResume()

    onPause()

    onStop()

    onDestroy()

    Because the Activity hasbeen explicitly told to quit

    and is being removed fromthe task (and history stack),onFreeze() is not called,and onDestroy() is

    reached.

  • 8/9/2019 Inside the Android Application Framework

    22/44

    Example: Dialog BoxCall sequence:

    onCreate()

    onStart()

    onResume()Despite appearances,dialog boxes are Views,

    and not Activities, so theyhave no effect on theowning Activitys lifecycle.

    / f C

  • 8/9/2019 Inside the Android Application Framework

    23/44

    Example: Transparent/Non-fullscreen Child

    Call sequence:

    onCreate()

    onStart()

    onResume()

    onFreeze()

    onPause()

    onResume()

    The new partial-screenwindow leaves a portion ofthe previous window visible,

    so onPause() is followed byonResume() (withoutonStop()) when the childcloses.

  • 8/9/2019 Inside the Android Application Framework

    24/44

    Example: Device Goes to Sleep

    Call sequence:

    onCreate()

    onStart()

    onResume()

    onFreeze()

    onPause()

    onResume()

    The device going to sleep is

    identical to a non-fullscreenActivity being launched ontop.

  • 8/9/2019 Inside the Android Application Framework

    25/44

    Threads on Android

    Overview

    Loopers

    Multi-thread Considerations

  • 8/9/2019 Inside the Android Application Framework

    26/44

    Threading OverviewEach process has one thread (by default)

    Most components share the single thread

    Services and ContentProviders sometimes do not

  • 8/9/2019 Inside the Android Application Framework

    27/44

    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

  • 8/9/2019 Inside the Android Application Framework

    28/44

    Threads and Loopers

    APK PackageProcessThreadLooper

    MessageQueue

    ThreadExternalService

    Calls

    IntentReceive

    r

    Activity

    Activity

    UIEvents

    SystemEvents

    LocalService

    Call

  • 8/9/2019 Inside the Android Application Framework

    29/44

    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

    Th d i Oth C t t

  • 8/9/2019 Inside the Android Application Framework

    30/44

    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

    S i Lif l

  • 8/9/2019 Inside the Android Application Framework

    31/44

    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

  • 8/9/2019 Inside the Android Application Framework

    32/44

    More on Processes

    Resource Management

    Processes & Security

    Controlling Processes

    P R M t

  • 8/9/2019 Inside the Android Application Framework

    33/44

    Process Resource Management

    Spawned by the special Zygote process

    Process + pre-warmed Dalvik VM == responsiveness

    Process runs under user ID unique to system

    Process + User ID == security

    P & S it

  • 8/9/2019 Inside the Android Application Framework

    34/44

    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

  • 8/9/2019 Inside the Android Application Framework

    35/44

    Inter-Process Communication

    Why??

    Process Transparency

    Binder in 30 Seconds

    IPC using Parcelables

    IPC using Bundles

    Android IDL

    Wh ??

  • 8/9/2019 Inside the Android Application Framework

    36/44

    Why??All this process/Activity/task stuff is confusing... why?

    Its 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.

    P T

  • 8/9/2019 Inside the Android Application Framework

    37/44

    Process TransparencyProcess management is transparent to code.

    ...almost. In some cases, its unavoidably visible.

    Lifecycle is seamless, but data sometimes isnt.

    Specific APIs send data across process boundaries.

    IPC O i

  • 8/9/2019 Inside the Android Application Framework

    38/44

    Kernel Process

    IPC Overview

    Binder

    Bundle

    IntentReceive

    r

    Activity

    CustomObjects

    Service

    Parcel

    Parcelable

    Bi d i 30 S d

  • 8/9/2019 Inside the Android Application Framework

    39/44

    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

    IPC P l bl

  • 8/9/2019 Inside the Android Application Framework

    40/44

    IPC - ParcelablesA Parcelable is a class which can marshal its state to somethingBinder can handle -- namely, a Parcel

    Standard Java serialization has semantics Parcelables dont need

    Supporting full serialization would mean wasting CPU cycles

    IPC B dl

  • 8/9/2019 Inside the Android Application Framework

    41/44

    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

    IPC AIDL

  • 8/9/2019 Inside the Android Application Framework

    42/44

    IPC - AIDLAndroid 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

  • 8/9/2019 Inside the Android Application Framework

    43/44

    Wrapping Up

    APKs are loose collections of components

    Tasks (AKA apps) are bags of componentinstances that span processes & APKs

    Managed lifecycles & IPC join the seams

  • 8/9/2019 Inside the Android Application Framework

    44/44

    Questions?