Introduction to concurrency in Android application development.
Describes about using AsyncTask and Handler. Contains information on AsyncTask's screen rotation issue
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