SlideShare a Scribd company logo
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.

 Every Android component has a managed lifecycle
Basics of an Android Application


 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

              Activity   Activity   Activity
   Activity


                          ContentProvider
    ContentProvider
                              Process

         Service               Service


       Process               Process
     APK Package           APK Package
Activities and Tasks

   Activity   Activity     Activity   Activity


                    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

Remains running until killed by the system
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 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

    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
Activity Lifecycle
Normal execution

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

    onPause: signals loss of focus and possible impending shutdown
Activity Lifecycle
Shutting down

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

    Not guaranteed to be called (and usually not, except on finish()...)
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
Example: Child Activity Launched
 Call sequence:
    onCreate()
    onStart()
    onResume()
    onFreeze()                   This is the “classic”
    onPause()
                                 scenario.
    onStop()
    onRestart()
    onStart(), onResume(), ...
Example: Child Activity + Process
Death
 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), ...
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)
Example: finish() Called
Call 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.
Example: Dialog Box
Call sequence:
   onCreate()
   onStart()
                 Despite appearances,
   onResume()    dialog boxes are Views,
                 and not Activities, so they
                 have no effect on the
                 owning Activity’s lifecycle.
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.
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.

Loopers cannot accommodate multi-threaded access
   They are designed to play nicely with MessageHandlers
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
Threads and Views
Views 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
Threads in Other Contexts
Services & ContentProviders sometimes run in their own threads
   ...but still in the same process
Components can create threads, but must handle thread-safety
Service Lifecycle
Started 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
More on Processes


 Resource Management
 Processes & Security
 Controlling Processes
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
Processes & Security
Each 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
Inter-Process Communication


 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



                    Intent      Activity      Service
                   Receive
                       r
Binder in 30 Seconds
All 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 - Parcelables
A 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
IPC - Bundles
Bundles 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
“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
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”
Questions?

More Related Content

What's hot

201913046 wahyu septiansyah network programing
201913046 wahyu septiansyah network programing201913046 wahyu septiansyah network programing
201913046 wahyu septiansyah network programing
wahyuseptiansyah
 
Servletand sessiontracking
Servletand sessiontrackingServletand sessiontracking
Servletand sessiontrackingvamsi krishna
 
Conf soat tests_unitaires_Mockito_jUnit_170113
Conf soat tests_unitaires_Mockito_jUnit_170113Conf soat tests_unitaires_Mockito_jUnit_170113
Conf soat tests_unitaires_Mockito_jUnit_170113
SOAT
 
Vaadin 7
Vaadin 7Vaadin 7
Vaadin 7
Joonas Lehtinen
 
Vaadin 7
Vaadin 7Vaadin 7
Vaadin 7
Joonas Lehtinen
 
ZooKeeper Recipes and Solutions
ZooKeeper Recipes and SolutionsZooKeeper Recipes and Solutions
ZooKeeper Recipes and Solutions
Jeff Smith
 
Node.js Event Loop & EventEmitter
Node.js Event Loop & EventEmitterNode.js Event Loop & EventEmitter
Node.js Event Loop & EventEmitter
Simen Li
 
Vaadin7
Vaadin7Vaadin7
Java 5 concurrency
Java 5 concurrencyJava 5 concurrency
Java 5 concurrencypriyank09
 
Rapid Network Application Development with Apache MINA
Rapid Network Application Development with Apache MINARapid Network Application Development with Apache MINA
Rapid Network Application Development with Apache MINA
trustinlee
 
Making Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVMMaking Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVM
Rafael Winterhalter
 

What's hot (12)

201913046 wahyu septiansyah network programing
201913046 wahyu septiansyah network programing201913046 wahyu septiansyah network programing
201913046 wahyu septiansyah network programing
 
Servletand sessiontracking
Servletand sessiontrackingServletand sessiontracking
Servletand sessiontracking
 
Conf soat tests_unitaires_Mockito_jUnit_170113
Conf soat tests_unitaires_Mockito_jUnit_170113Conf soat tests_unitaires_Mockito_jUnit_170113
Conf soat tests_unitaires_Mockito_jUnit_170113
 
Vaadin 7
Vaadin 7Vaadin 7
Vaadin 7
 
Vaadin 7
Vaadin 7Vaadin 7
Vaadin 7
 
ZooKeeper Recipes and Solutions
ZooKeeper Recipes and SolutionsZooKeeper Recipes and Solutions
ZooKeeper Recipes and Solutions
 
Node.js Event Loop & EventEmitter
Node.js Event Loop & EventEmitterNode.js Event Loop & EventEmitter
Node.js Event Loop & EventEmitter
 
Vaadin7
Vaadin7Vaadin7
Vaadin7
 
Java 5 concurrency
Java 5 concurrencyJava 5 concurrency
Java 5 concurrency
 
Rapid Network Application Development with Apache MINA
Rapid Network Application Development with Apache MINARapid Network Application Development with Apache MINA
Rapid Network Application Development with Apache MINA
 
Making Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVMMaking Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVM
 
What's new in Java EE 6
What's new in Java EE 6What's new in Java EE 6
What's new in Java EE 6
 

Similar to Inside the android_application_framework

Inside the Android application framework - Google I/O 2009
Inside the Android application framework - Google I/O 2009Inside the Android application framework - Google I/O 2009
Inside the Android application framework - Google I/O 2009
Viswanath J
 
Android Jumpstart Jfokus
Android Jumpstart JfokusAndroid Jumpstart Jfokus
Android Jumpstart Jfokus
Lars Vogel
 
Android os
Android osAndroid os
Android os
AGAM SHAH
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
cacois
 
Android OS
Android OSAndroid OS
Android OS
Vishal Sapariya
 
Linux Programming
Linux ProgrammingLinux Programming
Node
NodeNode
node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backend
David Padbury
 
Unit I- ANDROID OVERVIEW.ppt
Unit I- ANDROID OVERVIEW.pptUnit I- ANDROID OVERVIEW.ppt
Unit I- ANDROID OVERVIEW.ppt
dineshkumar periyasamy
 
Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Akshay Nagpurkar
 
Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Akshay Nagpurkar
 
Android Introduction on Java Forum Stuttgart 11
Android Introduction on Java Forum Stuttgart 11 Android Introduction on Java Forum Stuttgart 11
Android Introduction on Java Forum Stuttgart 11
Lars Vogel
 
jBPM
jBPMjBPM
Introduction to nodejs
Introduction to nodejsIntroduction to nodejs
Introduction to nodejsJames Carr
 
11_UNIX_Processes_Including_Select.ppt
11_UNIX_Processes_Including_Select.ppt11_UNIX_Processes_Including_Select.ppt
11_UNIX_Processes_Including_Select.ppt
SIDDHARTHANANDCSE202
 
Async programming and python
Async programming and pythonAsync programming and python
Async programming and python
Chetan Giridhar
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipse
anshunjain
 
The Ring programming language version 1.5.4 book - Part 14 of 185
The Ring programming language version 1.5.4 book - Part 14 of 185The Ring programming language version 1.5.4 book - Part 14 of 185
The Ring programming language version 1.5.4 book - Part 14 of 185
Mahmoud Samir Fayed
 

Similar to Inside the android_application_framework (20)

Inside the Android application framework - Google I/O 2009
Inside the Android application framework - Google I/O 2009Inside the Android application framework - Google I/O 2009
Inside the Android application framework - Google I/O 2009
 
Android Jumpstart Jfokus
Android Jumpstart JfokusAndroid Jumpstart Jfokus
Android Jumpstart Jfokus
 
Android os
Android osAndroid os
Android os
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
 
Android OS
Android OSAndroid OS
Android OS
 
Linux Programming
Linux ProgrammingLinux Programming
Linux Programming
 
Node
NodeNode
Node
 
concurrency
concurrencyconcurrency
concurrency
 
node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backend
 
Android
AndroidAndroid
Android
 
Unit I- ANDROID OVERVIEW.ppt
Unit I- ANDROID OVERVIEW.pptUnit I- ANDROID OVERVIEW.ppt
Unit I- ANDROID OVERVIEW.ppt
 
Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Ppl for students unit 4 and 5
Ppl for students unit 4 and 5
 
Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Ppl for students unit 4 and 5
Ppl for students unit 4 and 5
 
Android Introduction on Java Forum Stuttgart 11
Android Introduction on Java Forum Stuttgart 11 Android Introduction on Java Forum Stuttgart 11
Android Introduction on Java Forum Stuttgart 11
 
jBPM
jBPMjBPM
jBPM
 
Introduction to nodejs
Introduction to nodejsIntroduction to nodejs
Introduction to nodejs
 
11_UNIX_Processes_Including_Select.ppt
11_UNIX_Processes_Including_Select.ppt11_UNIX_Processes_Including_Select.ppt
11_UNIX_Processes_Including_Select.ppt
 
Async programming and python
Async programming and pythonAsync programming and python
Async programming and python
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipse
 
The Ring programming language version 1.5.4 book - Part 14 of 185
The Ring programming language version 1.5.4 book - Part 14 of 185The Ring programming language version 1.5.4 book - Part 14 of 185
The Ring programming language version 1.5.4 book - Part 14 of 185
 

Recently uploaded

Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
kitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptxkitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptx
datarid22
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
NelTorrente
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptxFresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
SriSurya50
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 

Recently uploaded (20)

Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
kitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptxkitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptx
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptxFresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 

Inside the android_application_framework

  • 1. Inside the Android Application Framework
  • 2. 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
  • 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. Basics of an Android Application Activities Tasks Processes
  • 5. 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
  • 6. Activities and Tasks Activity Activity Activity Activity ContentProvider ContentProvider Process Service Service Process Process APK Package APK Package
  • 7. Activities and Tasks Activity Activity Activity Activity Task ContentProvider ContentProvider Process Service Service Process Process APK Package APK Package
  • 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. 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. 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
  • 11. 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 Remains running until killed by the system
  • 12. More on Activities Activity Lifecycle Examples of Common Use Cases
  • 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. Activity Lifecycle Three 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. Activity Lifecycle Normal execution onFreeze(): save UI state (NOT intended to save persistent data) onPause: signals loss of focus and possible impending shutdown
  • 16. Activity Lifecycle Shutting down onStop()/onDestroy(): final shutdown and process termination Not guaranteed to be called (and usually not, except on finish()...)
  • 17. 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
  • 18. Example: Child Activity Launched Call sequence: onCreate() onStart() onResume() onFreeze() This is the “classic” onPause() scenario. onStop() onRestart() onStart(), onResume(), ...
  • 19. Example: Child Activity + Process Death 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. 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. Example: finish() Called Call 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. Example: Dialog Box Call 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. 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. 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. Threads on Android Overview Loopers Multi-thread Considerations
  • 26. Threading Overview Each process has one thread (by default) Most components share the single thread Services and ContentProviders sometimes do not
  • 27. 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. Loopers cannot accommodate multi-threaded access They are designed to play nicely with MessageHandlers
  • 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. Threads and Views Views 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
  • 30. Threads in Other Contexts Services & ContentProviders sometimes run in their own threads ...but still in the same process Components can create threads, but must handle thread-safety
  • 31. Service Lifecycle Started 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
  • 32. More on Processes Resource Management Processes & Security Controlling Processes
  • 33. 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
  • 34. Processes & Security Each 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
  • 35. Inter-Process Communication Why?? Process Transparency Binder in 30 Seconds IPC using Parcelables IPC using Bundles Android IDL
  • 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. 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.
  • 38. IPC Overview Kernel Process Binder Parcel Parcelable Bundle Custom Objects Intent Activity Service Receive r
  • 39. Binder in 30 Seconds All 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
  • 40. IPC - Parcelables A 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
  • 41. IPC - Bundles Bundles 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
  • 42. 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
  • 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”