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