SlideShare a Scribd company logo
Introduction to Android - Session 3
Today’s Manifest
Concurrency
● Android’s thread model
● Using concurrency
o AsyncTasks
o Handlers
● Adverse effects
Android Thread Model
● Android uses a single thread to do all work
● That single thread, runs the user interface
o Thus, its called the UI Thread
● All code we write in Activity lifecycle events /
Broadcasts / Services are running in the UI
Thread
Android Thread model
● Doing extensive work on the UI thread will
slow down UI components
o e.g. When the thread does a heavy database query,
clicking the buttons cannot be done.!
● UI will become unresponsive
● Happens because UI thread can’t refresh
items while doing other work on itself.
Android Thread model
● When the UI becomes unresponsive,
Android takes action
● First is to show the “Application Not
Responding” dialog (ANR)
● Happens within 5 seconds of unresponsive
UI
Android Thread model
● If the user choses to wait
o Android tolerates for another 20s
● The message will continue to occur
● To prevent that, we need to use any one of
multiple concurrency methods in Android
Lets start coding
Stage 1
● Setup simple task in the form of a
Thread.sleep()
● Try getting an output from that task and
putting it to UI
● Try clicking UI items while at it.
o See the ANR?
Stage 2
● So the UI thread is the problem!
● Create another Thread. Java to the rescue.!
● Try setting the UI.
o Yes, its supposed to be that way. Don’t panic
Stage 3
● UI components can be updated in UI thread
● So use runOnUiThread()
● Problem solved. Or...is it?
● Try to get repetitive tasks to run with an
update.
o Code looks ugly? Indeed.
Stage 4
● Use AsyncTask for the same process
o Click
o Notify
o Update
o Finish
AsyncTask
● Executes in a different Thread than the UI
thread.
● Suitable for small, couple of second
operations
● All AsyncTasks run in one Thread at the OS
level.
AsyncTask
● Four segments
o Pre execute
o Do in background
o On update
o Post execute
● All others except doInBackground() are
run on the UI thread.
● doInBackground() is run in the parallel
AsyncTask thread.
Stage 4
● Done deal.? Not so fast
● AsyncTask cannot be used in every case.
● Long running executions should be done
using ThreadPoolExecutor and
FutureTask
o Better left for another session
● To make matters worse, AsyncTask has
some issues as well.
Issues with AsyncTask
● Run the same app, but this time, rotate the
screen while the task is going.!
● In order to prevent that, we need to either
o Lock screen orientation (at least while in task)
o Use Fragments
● Of course we’re going to use Fragments.
Who asked that question?
Stage 5
● Create a dummy (blank) Fragment with no
UI, but our AsyncTask
● Make sure to call setRetainInstance(true);
● Use a local variable to re-initiate the
progress dialogs if needed.
Handler
● A Handler can be used to receive messages
from other threads
● The handler resides in the UI thread and
updates the UI components accordingly
Stage 6
● Use the usual Activity with a new Thread
● Run the Thread and pass a message to a
Handler in the Activity
Confused?
you should be….!
is here
https://github.com/tdevinda/DialogAndroidIntro_3
Check branches asynctask and handler
(you will need git)
Final Code
Introduction to Android - Session 3

More Related Content

PPTX
Introduction to Android
PDF
Export pdf with puppeteer
PDF
Live logger for Umbraco
PDF
Instant LAMP Stack with Vagrant and Puppet
PDF
Amplify - TDD on Android with Robolectric
ODP
Whatsmeteor a whatsApp like Chat App writen in Meteorjs
PPTX
Protractor
PDF
Debugging with VS2019
Introduction to Android
Export pdf with puppeteer
Live logger for Umbraco
Instant LAMP Stack with Vagrant and Puppet
Amplify - TDD on Android with Robolectric
Whatsmeteor a whatsApp like Chat App writen in Meteorjs
Protractor
Debugging with VS2019

Similar to Introduction to Android - Session 3 (20)

PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
PDF
Background threads, async communication and vaadin
PDF
Insights on Protractor testing
ODP
Shake that-fud-vrs5
PPTX
Full stack development
PPTX
Lecture #2 threading, networking & permissions final version #2
PDF
[Android] Multiple Background Threads
PDF
Mobi Vision 2.0
PPTX
Threading model in windows store apps
PPTX
Android Connecting to internet Part 2
ODP
Improving Cross Desktop Standard by Cedric BAIL (GNOME Asia Summit 2013)
PDF
TMPA-2017: Unity Application Testing Automation with Appium and Image Recogni...
PDF
Campkde 2010: KDE Plasma netbook
PPTX
Introduction to React Native
PDF
Node.js basics
PPTX
A sip of elixir
PDF
Introduction to Continuous Delivery
PPTX
An overview of node.js
PDF
New generation Linux distributions that companion with Geeko : opencocon and...
PPTX
Background Thread
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Background threads, async communication and vaadin
Insights on Protractor testing
Shake that-fud-vrs5
Full stack development
Lecture #2 threading, networking & permissions final version #2
[Android] Multiple Background Threads
Mobi Vision 2.0
Threading model in windows store apps
Android Connecting to internet Part 2
Improving Cross Desktop Standard by Cedric BAIL (GNOME Asia Summit 2013)
TMPA-2017: Unity Application Testing Automation with Appium and Image Recogni...
Campkde 2010: KDE Plasma netbook
Introduction to React Native
Node.js basics
A sip of elixir
Introduction to Continuous Delivery
An overview of node.js
New generation Linux distributions that companion with Geeko : opencocon and...
Background Thread
Ad

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Transform Your Business with a Software ERP System
PPTX
Introduction to Artificial Intelligence
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
System and Network Administration Chapter 2
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Essential Infomation Tech presentation.pptx
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
history of c programming in notes for students .pptx
PDF
medical staffing services at VALiNTRY
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Transform Your Business with a Software ERP System
Introduction to Artificial Intelligence
wealthsignaloriginal-com-DS-text-... (1).pdf
Nekopoi APK 2025 free lastest update
System and Network Administration Chapter 2
Upgrade and Innovation Strategies for SAP ERP Customers
VVF-Customer-Presentation2025-Ver1.9.pptx
Essential Infomation Tech presentation.pptx
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Internet Downloader Manager (IDM) Crack 6.42 Build 41
How Creative Agencies Leverage Project Management Software.pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Design an Analysis of Algorithms I-SECS-1021-03
history of c programming in notes for students .pptx
medical staffing services at VALiNTRY
Reimagine Home Health with the Power of Agentic AI​
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Ad

Introduction to Android - Session 3

  • 2. Today’s Manifest Concurrency ● Android’s thread model ● Using concurrency o AsyncTasks o Handlers ● Adverse effects
  • 3. Android Thread Model ● Android uses a single thread to do all work ● That single thread, runs the user interface o Thus, its called the UI Thread ● All code we write in Activity lifecycle events / Broadcasts / Services are running in the UI Thread
  • 4. Android Thread model ● Doing extensive work on the UI thread will slow down UI components o e.g. When the thread does a heavy database query, clicking the buttons cannot be done.! ● UI will become unresponsive ● Happens because UI thread can’t refresh items while doing other work on itself.
  • 5. Android Thread model ● When the UI becomes unresponsive, Android takes action ● First is to show the “Application Not Responding” dialog (ANR) ● Happens within 5 seconds of unresponsive UI
  • 6. Android Thread model ● If the user choses to wait o Android tolerates for another 20s ● The message will continue to occur ● To prevent that, we need to use any one of multiple concurrency methods in Android
  • 8. Stage 1 ● Setup simple task in the form of a Thread.sleep() ● Try getting an output from that task and putting it to UI ● Try clicking UI items while at it. o See the ANR?
  • 9. Stage 2 ● So the UI thread is the problem! ● Create another Thread. Java to the rescue.! ● Try setting the UI. o Yes, its supposed to be that way. Don’t panic
  • 10. Stage 3 ● UI components can be updated in UI thread ● So use runOnUiThread() ● Problem solved. Or...is it? ● Try to get repetitive tasks to run with an update. o Code looks ugly? Indeed.
  • 11. Stage 4 ● Use AsyncTask for the same process o Click o Notify o Update o Finish
  • 12. AsyncTask ● Executes in a different Thread than the UI thread. ● Suitable for small, couple of second operations ● All AsyncTasks run in one Thread at the OS level.
  • 13. AsyncTask ● Four segments o Pre execute o Do in background o On update o Post execute ● All others except doInBackground() are run on the UI thread. ● doInBackground() is run in the parallel AsyncTask thread.
  • 14. Stage 4 ● Done deal.? Not so fast ● AsyncTask cannot be used in every case. ● Long running executions should be done using ThreadPoolExecutor and FutureTask o Better left for another session ● To make matters worse, AsyncTask has some issues as well.
  • 15. Issues with AsyncTask ● Run the same app, but this time, rotate the screen while the task is going.! ● In order to prevent that, we need to either o Lock screen orientation (at least while in task) o Use Fragments ● Of course we’re going to use Fragments. Who asked that question?
  • 16. Stage 5 ● Create a dummy (blank) Fragment with no UI, but our AsyncTask ● Make sure to call setRetainInstance(true); ● Use a local variable to re-initiate the progress dialogs if needed.
  • 17. Handler ● A Handler can be used to receive messages from other threads ● The handler resides in the UI thread and updates the UI components accordingly
  • 18. Stage 6 ● Use the usual Activity with a new Thread ● Run the Thread and pass a message to a Handler in the Activity