SlideShare a Scribd company logo
Android activity launch modes and tasks Gonçalo Silva / Novoda
 
Co-organizer ~1250 members
Activity launch modes and tasks..
What is an Activity?
Main building block of Android applications
HelloWorldActivity.java public   class  HelloWorldActivity  extends  Activity { .... }
AndroidManifest.xml <? xml   version = &quot;1.0&quot;   encoding = &quot;utf-8&quot; ?> < manifest  xmlns:android = &quot;http://schemas.android.com/apk/res/android&quot; package = &quot;com.novoda.jax&quot; android:versionCode = &quot;1&quot; android:versionName = &quot;1.0&quot;   > < application   > < activity android:name = &quot;.HelloWorldActivity&quot;   />   </ application > </ manifest >
Activity task Sequence of related activities a user follows
 
 
 
 
Activity stack / back stack
How are activites started?
Messaging facility for late run-time binding between components Intents!
Intent object Action Data – URI and/or MIME type Category Extras – key/value pairs Component name Flags
Explicit intent resolution Intent intent =  new  Intent( this , HelloWorldActivity. class ); startActivity(intent);
Implicit intent resolution Uri google = Uri. parse ( &quot;http://www.google.com&quot; ); Intent intent =  new  Intent( Intent. ACTION_VIEW , google); startActivity(intent);
Intent filter < activity   android:name = &quot;.SimpleBrowser&quot;   > < intent-filter  > < action   android:name = &quot;android.intent.action.VIEW&quot;   /> < category   android:name = &quot;android.intent.category.DEFAULT&quot;   /> < data   android:scheme = &quot;http&quot;   /> </ intent-filter > </activity>
Intents are great! Loosely coupled activities Specify your behaviour as intentions
Activity re-use Starting an activity in a different application
Task Twitter
Task Twitter
Task Twitter Gmail
Activity re-use Some activities are started in a different task
Task A Gmail
Task A Gmail Task B Browser
Activity re-use Allows user to resume after leaving original task Why different tasks?
How can we resume an activity?
 
Resuming an activity
Resuming a “killed” activity
How can we choose a specific task behaviour for our activities?
Activity launch modes Define how a new instance of an activity is associated with the current task
Four launch modes standard singleTop singleTask singleInstance
Defined in the manifest < activity android:launchMode = &quot;singleTop&quot; android:name = &quot;.HelloWorldActivity&quot;   />
Set as a flag in an intent Intent intent =  new  Intent( this , HelloWorldActivity. class ); intent.addFlags(Intent. FLAG_ACTIVITY_SINGLE_TOP ); startActivity(intent);
Demo App!!! Search the market for: launchmode
standard – default launch mode
singleTop A new activity X will not be started if there is an activity X on top of the stack onNewIntent() will be called instead of the normal activity lifecycle
singleTask Created at the root of a new task Only one instance of the activity can exist at a time onNewIntent() is called if an instance of the activity already exists
singleInstance Like singleTop but the only member of its task Any activities started will open in a separate task.
Examples of launchModes singleTop: Dashboard, Search singleTask: Browser, Google Maps singleInstance: Home screen
Intent flags and launchModes
FLAG_ACTIVITY_SINGLE_TOP
FLAG_ACTIVITY_MULTIPLE_TASK FLAG_ACTIVITY_NEW_TASK Force an activity to a new task Not recommended!
FLAG_ACTIVITY_NO_HISTORY
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_REORDER_TO_FRONT
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS The new activity is not kept in the list of recently launched activities
FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
Other manifest attributes to help deal with activities and tasks
android:taskAffinity Useful to ensure certain activities are always in the same task By default, all activities in an application have the same affinity singleTask Gotcha!!!
android:allowTaskReparenting Next time an activity comes to the front, move it to a task it has an affinity for Eg: email and browser
android:clearTaskOnLaunch Clear all activities from the task except the root, when launching the app from the home screen Only honoured for activities that start a new task Can be combined with “allowTaskReparenting”
android:alwaysRetainTaskState The state of the task is mantained by the system, even after a long period of time Only honoured for activities that start a new task Eg: browser tabs
android:finishOnTaskLaunch Shut down an instance of an activity when its task is resumed Trumps android:alwaysRetainTaskState
singleTask and singleInstance change the UI flow Use with caution!
singleTask UI flow
 
 
 
singleInstance UI flow
 
 
 
 
 
Two launcher icons
Provide a launcher icon for each singleTask / singleInstance activity
Another disadvantage:  startActivityForResult()
Recommended launchModes: standard and singleTop with intent flags combinations will suit most use cases
To recap: Activities and how they live in tasks Intents and re-using activities in other apps Activity lifecycle and the four launchmodes Intent flags and manifest attributes singleTask and singleInstance disadvantages
Thank you for listening! Any Questions?

More Related Content

PDF
Manipulating Android tasks and back stack
PDF
Android activity
PPT
Parceable serializable
PPTX
04 activities and activity life cycle
PDF
Angular - Chapter 4 - Data and Event Handling
PDF
Android: Intent, Intent Filter, Broadcast Receivers
PDF
Android activity
PPTX
Android data binding
Manipulating Android tasks and back stack
Android activity
Parceable serializable
04 activities and activity life cycle
Angular - Chapter 4 - Data and Event Handling
Android: Intent, Intent Filter, Broadcast Receivers
Android activity
Android data binding

What's hot (20)

PDF
AIDL - Android Interface Definition Language
PPTX
Jetpack Compose.pptx
PPTX
Broadcast Receiver
PDF
Introduction to Kotlin coroutines
PPTX
Android jetpack compose | Declarative UI
PPT
android activity
PDF
Introduction to kotlin
PDF
Analysing in depth work manager
PPT
Android lifecycle
PDF
Python functions
PDF
Android animation
PPTX
Android Services
PPTX
Android Intent.pptx
PPSX
Introduction to .net framework
PPTX
Design Pattern - Singleton Pattern
PDF
Android Basic Components
PPTX
PDF
Android Threading
PDF
Angular Dependency Injection
PPTX
Kotlin Basics & Introduction to Jetpack Compose.pptx
AIDL - Android Interface Definition Language
Jetpack Compose.pptx
Broadcast Receiver
Introduction to Kotlin coroutines
Android jetpack compose | Declarative UI
android activity
Introduction to kotlin
Analysing in depth work manager
Android lifecycle
Python functions
Android animation
Android Services
Android Intent.pptx
Introduction to .net framework
Design Pattern - Singleton Pattern
Android Basic Components
Android Threading
Angular Dependency Injection
Kotlin Basics & Introduction to Jetpack Compose.pptx
Ad

Viewers also liked (20)

PDF
Managing Activity Backstack
PPTX
The android activity lifecycle
PDF
02 programmation mobile - android - (activity, view, fragment)
PPTX
Android Activity Transition(ShareElement)
PPTX
Android activity lifecycle
PPTX
Android ppt
PPT
Android activity, service, and broadcast recievers
PPTX
IPC: AIDL is not a curse
PPTX
Android case: 5 perguntas + 1 sobre desenvolvimento para Android
PPT
Desenvolvimento para Android
ODP
Meu primeiro app nativo para Android - Minicurso SCTI UENF
PDF
Desenvolvimento android
PPTX
1ª aula - Teste
PPTX
Criando app mobile com Google Android - Software Freedom Day 2015
PPT
Pie.157055.ioc grupo 3 - android e i os
PPTX
Google android Activity lifecycle
PDF
Android Logging System
ODP
Aula teste ETEC - Analise de Programacao
PPTX
Android service, aidl - day 1
PPTX
Android AIDL Concept
Managing Activity Backstack
The android activity lifecycle
02 programmation mobile - android - (activity, view, fragment)
Android Activity Transition(ShareElement)
Android activity lifecycle
Android ppt
Android activity, service, and broadcast recievers
IPC: AIDL is not a curse
Android case: 5 perguntas + 1 sobre desenvolvimento para Android
Desenvolvimento para Android
Meu primeiro app nativo para Android - Minicurso SCTI UENF
Desenvolvimento android
1ª aula - Teste
Criando app mobile com Google Android - Software Freedom Day 2015
Pie.157055.ioc grupo 3 - android e i os
Google android Activity lifecycle
Android Logging System
Aula teste ETEC - Analise de Programacao
Android service, aidl - day 1
Android AIDL Concept
Ad

Similar to Android | Android Activity Launch Modes and Tasks | Gonçalo Silva (20)

PPTX
Android Developer Training
PDF
Android platform activity
PDF
The activity class
PDF
The activity class
ODP
Android App Development - 02 Activity and intent
PPTX
Android activity
PDF
Lecture 3 getting active through activities
PPTX
Unit 5 Activity and Activity Life Cycle.pptx
PDF
android_mod_3.useful for bca students for their last sem
PDF
Android development - Activities, Views & Intents
PDF
Android development Training Programme Day 2
PPT
Day 4: Android: Getting Active through Activities
DOCX
Android building blocks and application life cycle-chapter3
PDF
Android activities & views
PPT
Multiple Activity and Navigation Primer
PPTX
Unit-1.2 Android-Activities, Fragments, and Intents (1).pptx
PPT
Day 3: Getting Active Through Activities
PPT
Day 3: Getting Active Through Activities
ODP
Anatomy of android application
PDF
Android application development workshop day1
Android Developer Training
Android platform activity
The activity class
The activity class
Android App Development - 02 Activity and intent
Android activity
Lecture 3 getting active through activities
Unit 5 Activity and Activity Life Cycle.pptx
android_mod_3.useful for bca students for their last sem
Android development - Activities, Views & Intents
Android development Training Programme Day 2
Day 4: Android: Getting Active through Activities
Android building blocks and application life cycle-chapter3
Android activities & views
Multiple Activity and Navigation Primer
Unit-1.2 Android-Activities, Fragments, and Intents (1).pptx
Day 3: Getting Active Through Activities
Day 3: Getting Active Through Activities
Anatomy of android application
Android application development workshop day1

More from JAX London (20)

PDF
Java Tech & Tools | Continuous Delivery - the Writing is on the Wall | John S...
ODP
Java Tech & Tools | Mapping, GIS and Geolocating Data in Java | Joachim Van d...
PDF
Keynote | Middleware Everywhere - Ready for Mobile and Cloud | Dr. Mark Little
PDF
Spring Day | WaveMaker - Spring Roo - SpringSource Tool Suite: Choosing the R...
PDF
Spring Day | Behind the Scenes at Spring Batch | Dave Syer
PDF
Spring Day | Spring 3.1 in a Nutshell | Sam Brannen
PDF
Spring Day | Identity Management with Spring Security | Dave Syer
PDF
Spring Day | Spring and Scala | Eberhard Wolff
PDF
Spring Day | Data Access 2.0? Please Welcome Spring Data! | Oliver Gierke
PPT
Keynote | The Rise and Fall and Rise of Java | James Governor
ODP
Java Tech & Tools | OSGi Best Practices | Emily Jiang
PPTX
Java Tech & Tools | Beyond the Data Grid: Coherence, Normalisation, Joins and...
PDF
Java Tech & Tools | Big Blobs: Moving Big Data In and Out of the Cloud | Adri...
PDF
Java Tech & Tools | Social Media in Programming in Java | Khanderao Kand
PDF
Java Tech & Tools | Just Keep Passing the Message | Russel Winder
PDF
Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook
PDF
Java Tech & Tools | Deploying Java & Play Framework Apps to the Cloud | Sande...
ODP
Java EE | Modular EJBs for Enterprise OSGi | Tim Ward
PDF
Java EE | Apache TomEE - Java EE Web Profile on Tomcat | Jonathan Gallimore
KEY
Java Core | Understanding the Disruptor: a Beginner's Guide to Hardcore Concu...
Java Tech & Tools | Continuous Delivery - the Writing is on the Wall | John S...
Java Tech & Tools | Mapping, GIS and Geolocating Data in Java | Joachim Van d...
Keynote | Middleware Everywhere - Ready for Mobile and Cloud | Dr. Mark Little
Spring Day | WaveMaker - Spring Roo - SpringSource Tool Suite: Choosing the R...
Spring Day | Behind the Scenes at Spring Batch | Dave Syer
Spring Day | Spring 3.1 in a Nutshell | Sam Brannen
Spring Day | Identity Management with Spring Security | Dave Syer
Spring Day | Spring and Scala | Eberhard Wolff
Spring Day | Data Access 2.0? Please Welcome Spring Data! | Oliver Gierke
Keynote | The Rise and Fall and Rise of Java | James Governor
Java Tech & Tools | OSGi Best Practices | Emily Jiang
Java Tech & Tools | Beyond the Data Grid: Coherence, Normalisation, Joins and...
Java Tech & Tools | Big Blobs: Moving Big Data In and Out of the Cloud | Adri...
Java Tech & Tools | Social Media in Programming in Java | Khanderao Kand
Java Tech & Tools | Just Keep Passing the Message | Russel Winder
Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook
Java Tech & Tools | Deploying Java & Play Framework Apps to the Cloud | Sande...
Java EE | Modular EJBs for Enterprise OSGi | Tim Ward
Java EE | Apache TomEE - Java EE Web Profile on Tomcat | Jonathan Gallimore
Java Core | Understanding the Disruptor: a Beginner's Guide to Hardcore Concu...

Recently uploaded (20)

PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Cloud computing and distributed systems.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPT
Teaching material agriculture food technology
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
Dropbox Q2 2025 Financial Results & Investor Presentation
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
sap open course for s4hana steps from ECC to s4
NewMind AI Weekly Chronicles - August'25 Week I
Cloud computing and distributed systems.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Teaching material agriculture food technology
Digital-Transformation-Roadmap-for-Companies.pptx
Spectral efficient network and resource selection model in 5G networks
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
MYSQL Presentation for SQL database connectivity
Encapsulation_ Review paper, used for researhc scholars
Advanced methodologies resolving dimensionality complications for autism neur...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Chapter 3 Spatial Domain Image Processing.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Per capita expenditure prediction using model stacking based on satellite ima...

Android | Android Activity Launch Modes and Tasks | Gonçalo Silva