English subtitles

← Sunshine in the Background - Developing Android Apps

Get Embed Code
10 Languages

Showing Revision 13 created 05/24/2016 by Udacity Robot.

  1. Remember back in lesson two
    when Katherine used
  2. an AsyncTask hooked up to
    a refresh button to update our data?
  3. Rato talked about how
    that was a bad idea
  4. because the AsyncTask is not tied
    to the activity life cycle.
  5. The virtual machine will hold on
    to the activity object
  6. as long as the AsyncTask is running,
    even after Android
  7. has called onDestroy for the activity
    and expect it to be discarded.
  8. If you rotate your phone,
    the behavior is to destroy
  9. your activity and instantiate a new one.
  10. The naive AsyncTask implementation
    now has two threads
  11. trying to perform the same update,
    and so forth.
  12. The point is, it's not
    the best pattern
  13. for a potentially
    very long background operation,
  14. such as fetching from web services.
  15. If you leave the app,
    the AsyncTask will continue to run
  16. for as long as your process is kept alive,
  17. but will run at a low priority,
    and your process
  18. will be the first thing
    to be killed if the device
  19. needs more resources.
  20. And there's a bigger problem.
  21. Your app has to be visible
    and running in the foreground
  22. to instantiate the task
    in the first place.
  23. Because we started a task,
  24. to update the weather
    when we started the app,
  25. this can have undesirable behavior
    if the weather changes rapidly.
  26. So now we're going to learn
    the right way to perform updates.
  27. We'll want to automate
    the process while the app
  28. is in the foreground.
  29. But even more importantly,
    we want the app to get
  30. regular updates in the background
    with minimal battery drain.
  31. That will be especially important
    later in this lesson
  32. when we introduce
    weather notifications.