Return to Video

Move to AsyncTask - Developing Android Apps

  • 0:00 - 0:02
    Let's apply what we just learned by opening up
  • 0:02 - 0:06
    the MainActivity.java file within our project. We're going to take
  • 0:06 - 0:09
    this networking code snippet and move it over to
  • 0:09 - 0:11
    it's own AsyncTask, so it runs in a background
  • 0:11 - 0:14
    thread. The task is going to be defined within
  • 0:14 - 0:18
    this fragment class. But speaking of which, it's actually
  • 0:18 - 0:21
    still called PlaceholderFragment. Let's do a little bit of
  • 0:21 - 0:25
    refactoring now by giving it a real name. Let's call
  • 0:25 - 0:28
    it ForecastFragment. And then you can update it
  • 0:28 - 0:32
    in other appropriate places as well. We can also
  • 0:32 - 0:35
    move ForecastFragment into its own file that way
  • 0:35 - 0:38
    the MainActivity won't get so long and cumbersome. Within
  • 0:38 - 0:41
    ForecastFragment we should define a new inner class
  • 0:41 - 0:45
    called FetchWeatherTask which extends from AsyncTask. And then you
  • 0:45 - 0:49
    can move the networking code snippet here. After you
  • 0:49 - 0:51
    make the changes and run your app, it should
  • 0:51 - 0:54
    look something like this. There really should be no difference except it
  • 0:54 - 0:57
    doesn't crash now. In the next step we will add code to execute
  • 0:57 - 1:01
    the task. And later in the lesson, we'll do JSON parsing and updating
  • 1:01 - 1:05
    the UI. In the meantime though, make these initial changes to your code.
Title:
Move to AsyncTask - Developing Android Apps
Description:

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

English subtitles

Revisions Compare revisions