Building the Loader6:04 with Ben Deitch
In this video we'll build our own AsyncTaskLoader to handle loading our images in a way that's lifecycle-aware!
We've just seen why we might want to use a loader. 0:00 Instead of an async task. 0:02 But we still don't know how to use a loader. 0:04 Well, lucky for us. 0:07 There's a special loader class. 0:08 Specifically for dealing with async tasks. 0:10 And it's called AsyncTaskLoader. 0:13 So, let's refactor our code to use an AsyncTaskLoader. 0:15 Instead of an AsyncTask. 0:18 Let's create a new class named MyAsyncTaskLoader. 0:21 And let's make it extend the support version of AsyncTaskLoader. 0:32 Next, we need to specify what we want to get back from our loader. 0:43 So since we're looking for an array of bitmaps. 0:48 Let's add a bitmap array as the type of our AsyncTaskLoader. 0:50 Then let's use Alt+Enter. 1:01 To implement the loadInBackground method. 1:03 And use it again to add a constructor. 1:06 Now that we've got our loader. 1:11 Let's start downloading. 1:13 Over in MainActivity. 1:14 Let's copy the code inside the doInBackground method. 1:18 And paste it into the loadInBackground method. 1:24 Then, since we're just using the same URL over and over. 1:31 Let's make a few changes to our code. 1:35 First, let's create a new private int field, Named imageCount. 1:39 And set it equal to 10. 1:48 Then, inside loadInBackground. 1:53 Instead of using urls.length. 1:55 Let's just use imageCount. 1:57 Next, to get our URL into our loader. 2:12 Let's add a URL field. 2:16 Private URL url. 2:19 And let's populate it using the constructor. 2:24 And Alt + Enter to import it. 2:27 So let's pass in a URL. 2:31 Which we can call url. 2:33 And then after the call the super set this.url equal to url. 2:35 Finally, let's use our new URL field. 2:45 In the loadInBackground method. 2:48 So instead of URLs at index i. 2:50 We'll just use our URL. 2:53 All right, now we've got everything. 2:55 Except for updating our progress. 2:58 But unfortunately, AsyncTaskLoaders don't give us. 3:01 A good way to do progress updates. 3:04 So we're just going to have to do without them. 3:06 However, that doesn't mean we can't show anything. 3:11 We can still show an indeterminate progress bar. 3:15 Let's go into activitymain.XML. 3:18 And add our indeterminate property to our progress bar. 3:22 So android: indeterminate="true". 3:26 Now whenever our progress bar is visible. 3:36 It will show a loading animation. 3:38 So all we need to do. 3:40 Is set it to visible when we start loading. 3:41 And hide it when we're done. 3:44 But before we do that. 3:46 Let's finish up our AsyncTaskLoader. 3:48 Right now if we were to use this loader. 3:51 And we rotated the device. 3:53 We'd start downloading a new set of images. 3:55 Hold on a minute. 3:59 Isn't that one of the reasons we're using a loader? 4:00 So it would only download the data once. 4:02 That's right. But loaders don't do that by default. 4:05 We have to implement it. 4:08 Let's start by creating a private array to hold our bitmaps. 4:10 And let's name it BitmapsCache. 4:21 Then, below our loadInBackground method, 4:27 Let's use Ctrl+O to override the deliverResult method. 4:33 This method is called when the work is done. 4:40 And gives us a chance to do something. 4:43 With the result from within our loader. 4:45 It's a great place to store off the results for later. 4:47 So at the bottom of this function. 4:51 Let's store our data and our bitmapsCache. 4:53 So bitmapsCache = data. 4:57 Next let's go above the loadInBackground method. 5:02 And use Ctrl+O again. 5:07 To override the onStartLoading method. 5:09 And get rid of the call to super. 5:14 This method is called when we first start the loader. 5:16 And is where we need to decide. 5:19 If we're going to use the bitmaps in our cache. 5:21 Or load new ones. 5:23 So let's start by checking if we have a cache. 5:25 if bitmapsCache != null. 5:27 And if we do have a cache. 5:34 Then let's just call straight through. 5:35 To the deliverResult method with our cache, deliverResult. 5:37 And pass in our bitmapCache. 5:42 Otherwise if we don't have a bitmapCache. 5:45 Then let's just call the forceLoad method. 5:48 To kick off our download. 5:53 Awesome, our AsyncTaskLoader is primed and ready to go. 5:56 In the next video. 6:00 We'll finally get rid of the AsyncTask. 6:00 And start using our loader 6:02
You need to sign up for Treehouse in order to download course files.Sign up