Return to Video

Sunshine in the Background - Developing Android Apps

  • 0:00 - 0:04
    Remember back in lesson two when Katherine used a
  • 0:04 - 0:07
    AsyncTask hooked up to a refresh button to update
  • 0:07 - 0:09
    our data? Rato talked about how that was a
  • 0:09 - 0:13
    bad idea, because the AsyncTask is not tied to
  • 0:13 - 0:17
    the activity life cycle. The virtual machine will hold
  • 0:17 - 0:19
    on to the activity object as long as the
  • 0:19 - 0:23
    AsyncTask is running, even after Android has called onDestroy
  • 0:23 - 0:26
    for the activity and expect it to be discarded.
  • 0:26 - 0:28
    If you rotate your phone, the behavior is to
  • 0:28 - 0:32
    destroy your activity and instantiate a new one. The
  • 0:32 - 0:35
    naive AsyncTask implementation now has two threads trying to
  • 0:35 - 0:39
    perform the same update, and so forth. The point is,
  • 0:39 - 0:42
    it's not the best pattern for a potentially very
  • 0:42 - 0:46
    long background operation, such as fetching from web services. If
  • 0:46 - 0:48
    you leave the app, the asynctask will continue to
  • 0:48 - 0:51
    run for as long as your process is kept alive,
  • 0:51 - 0:54
    but will run at a low priority, and your process
  • 0:54 - 0:56
    will be the first thing to be killed if the device
  • 0:56 - 1:00
    needs more resources. And there's a bigger problem. Your app
  • 1:00 - 1:03
    has to be visible and running in the foreground to instantiate
  • 1:03 - 1:06
    the task in the first place. Because we started a
  • 1:06 - 1:09
    task, to update the weather when we started the app, this
  • 1:09 - 1:13
    can have undesirable behavior if the weather changes rapidly. So
  • 1:13 - 1:16
    now we're going to learn the right way to perform updates.
  • 1:16 - 1:19
    We'll want to automate the process while the app is in
  • 1:19 - 1:23
    the foreground. But even more importantly, we want the app to get
  • 1:23 - 1:27
    regular updates in the background with minimal battery drain. That will
  • 1:27 - 1:29
    be especially important later in this
  • 1:29 - 1:31
    lesson when we introduce weather notifications.
Title:
Sunshine in the Background - Developing Android Apps
Description:

more » « less
Video Language:
English
Team:
Udacity
Project:
UD853 - Developing Android Apps
Duration:
01:32

English subtitles

Revisions Compare revisions