Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Using the Loader4:48 with Ben Deitch
In this video we'll see how to use our newly creating AsyncTaskLoader instead of an AsyncTask!
We just finished creating our loader, but we still haven't seen how to use it. 0:00 But before we get to that, 0:04 it looks like I forgot to delete this percentProgress line. 0:06 So let's do that. 0:09 Okay, now it's time to get back to deleting our AsyncTaskClass. 0:12 Over in MainActivity, let's start by deleting our AsyncTaskClass. 0:16 And let's also delete this new MyAsyncTask line. 0:23 Next, we need to implement a few callbacks to help us interact with our loader. 0:32 Specifically, we need to implement LoaderCallbacks, and 0:37 make sure we're picking the option from the support library. 0:42 And let's use a bitmap array for our type parameter again. 0:47 Then let's use Alt+Enter to implement the three required methods. 0:53 The first method is onCreateLoader. 1:00 In this method, we need to create a new instance of our loader and return it. 1:03 So since our loader requires a URL as a parameter, 1:08 let's first cut and paste the URL code into the top of this method. 1:11 Then, right below the URL, let's return a new instance of our loader. 1:23 return new MyAsyncTaskLoader, and 1:29 passing in this for the context and url for the URL. 1:33 I'd love to get rid of this return null as well, but 1:43 even though our URL isn't malformed, we've still gotta catch the exception, 1:46 which means we need to have a return down here. 1:51 Well. Moving on to onLoadFinished, 1:54 this method is called when your work is done and is where we should update the UI. 1:57 So let's first update the data, our adapter is based on. 2:02 myAdapter.bitmaps = data. 2:08 And then let's notify our adapter that the data has been updated. 2:13 myAdapter.notifyDataSetChanged. 2:18 Nice. 2:21 The last method is onLoaderReset. 2:22 If your loader's been reset, then it no longer contains any data. 2:26 So if you've got any references to data in your loader, 2:30 you'd want to clean those up in this method. 2:33 Since we don't have any way to reset our loader, we're fine leaving this empty. 2:36 Now that we've implemented our loader callbacks, 2:41 all that's left is to start our loader. 2:43 At the bottom of onCreate. 2:45 Let's call getSupportLoaderManager().initLoader(). 2:51 Then let's pass in an arbitrary number for the ID, I'll choose 1. 3:01 And an arguments bundle, which we don't have, so let's just pass in null. 3:07 And finally a LoaderCallbacks object, 3:12 which we've implemented right here in this activity. 3:14 So let's just pass in this. 3:17 And before we forget, let's make sure to show our progress bar while we're loading 3:22 and hide it once we're done. 3:26 So at the top of OnCreateLoader, 3:28 let's add progressBar.setVisibility to View.VISIBLE. 3:32 And then at the top of onLoadFinished, Let's set it to gone. 3:39 progressBar.setVisibility(View.GONE); All right, time for the moment of truth. 3:45 Let's run the app and see what happens. 3:55 There's our progress bar, and now we've got our images. 4:02 And if we rotate the app, Awesome, it's the same images! 4:06 So instead of re-downloading all of those images, we just used our cached images and 4:13 made for a much better user experience. 4:18 When you've gotta do something in the background and 4:22 you're worried about the activity lifecycle, just use a loader. 4:24 Since a loader will stay alive through a rotation, it's a great 4:28 place to do your loading and keep any data safe from being destroyed in an activity. 4:30 If you'd like to learn more about loaders, check out the teacher's notes below. 4:35 And as always, if you've got any questions or would like to try your hand at 4:39 answering someone else's question, be sure to check out the community. 4:42 Until next time. 4:46
You need to sign up for Treehouse in order to download course files.Sign up