Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to Android - Session 3

274 views

Published on

Introduction to concurrency in Android application development.
Describes about using AsyncTask and Handler. Contains information on AsyncTask's screen rotation issue

Published in: Software
  • Be the first to comment

Introduction to Android - Session 3

  1. 1. Today’s Manifest Concurrency ● Android’s thread model ● Using concurrency o AsyncTasks o Handlers ● Adverse effects
  2. 2. 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
  3. 3. 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.
  4. 4. 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
  5. 5. 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
  6. 6. Lets start coding
  7. 7. 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?
  8. 8. 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
  9. 9. 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.
  10. 10. Stage 4 ● Use AsyncTask for the same process o Click o Notify o Update o Finish
  11. 11. 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.
  12. 12. 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.
  13. 13. 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.
  14. 14. 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?
  15. 15. 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.
  16. 16. 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
  17. 17. Stage 6 ● Use the usual Activity with a new Thread ● Run the Thread and pass a message to a Handler in the Activity
  18. 18. Confused? you should be….!
  19. 19. is here https://github.com/tdevinda/DialogAndroidIntro_3 Check branches asynctask and handler (you will need git) Final Code

×