Return to Video

Main Thread vs Background Thread - Developing Android Apps

  • 0:00 - 0:02
    When we said that the framework didn't want us
  • 0:02 - 0:05
    to run network operations on the main thread, what
  • 0:05 - 0:09
    is the main thread? Well, Android apps run by
  • 0:09 - 0:13
    default on the main thread, also called the UI thread.
  • 0:13 - 0:15
    It handles all the user input as well as
  • 0:15 - 0:18
    the output, such as screen drawing. Thus we want
  • 0:18 - 0:22
    to avoid any time-consuming operations here, otherwise the URI
  • 0:22 - 0:25
    is going to stutter. Instead, kick off a background worker
  • 0:25 - 0:27
    thread if you have to do some long-running
  • 0:27 - 0:32
    work. This includes doing network calls, decoding bitmaps, or
  • 0:32 - 0:36
    reading, and writing from the database. Okay. So, somehow,
  • 0:36 - 0:38
    we have to move the networking code off the
  • 0:38 - 0:40
    main thread. But how are we going to do
  • 0:40 - 0:43
    that? Well there are several options, but let's look
  • 0:43 - 0:47
    for the name of the Android class that simplifies
  • 0:47 - 0:51
    background thread creation and UI thread synchronization, so that
  • 0:51 - 0:53
    the results from the background work will come back onto the
  • 0:53 - 0:56
    main thread, and then we can use it to update our
  • 0:56 - 1:00
    UI. Search online and figure out the answer to this question
  • 1:00 - 1:03
    and then enter the class name in the box. Here's some
  • 1:03 - 1:05
    advice for you. If you ever get stuck on how to
  • 1:05 - 1:07
    do something in Android and you can't find it on the
  • 1:07 - 1:12
    developer's site you can try to check stackoverflow.com. It's a question
  • 1:12 - 1:17
    and answer site that many Android developers use as a valuable resource.
  • 1:17 - 1:19
    So chances are likely that someone has
  • 1:19 - 1:21
    already asked a similar question that you have.
Title:
Main Thread vs Background Thread - Developing Android Apps
Description:

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

English subtitles

Revisions Compare revisions