Introduction to Android - Session 3

21

Transcript of Introduction to Android - Session 3

Today’s Manifest

Concurrency● Android’s thread model● Using concurrency

o AsyncTaskso 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 componentso 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 waito 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 processo Clicko Notifyo Updateo 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 segmentso Pre executeo Do in backgroundo On updateo 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 FutureTasko 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 eithero 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 herehttps://github.com/tdevinda/DialogAndroidIntro_3

Check branches asynctask and handler

(you will need git)

Final Code