1 00:00:00,500 --> 00:00:04,810 We just finished creating our loader, but we still haven't seen how to use it. 2 00:00:04,810 --> 00:00:06,030 But before we get to that, 3 00:00:06,030 --> 00:00:09,100 it looks like I forgot to delete this percentProgress line. 4 00:00:09,100 --> 00:00:09,630 So let's do that. 5 00:00:12,260 --> 00:00:16,690 Okay, now it's time to get back to deleting our AsyncTaskClass. 6 00:00:16,690 --> 00:00:20,512 Over in MainActivity, let's start by deleting our AsyncTaskClass. 7 00:00:23,943 --> 00:00:26,879 And let's also delete this new MyAsyncTask line. 8 00:00:32,255 --> 00:00:37,064 Next, we need to implement a few callbacks to help us interact with our loader. 9 00:00:37,064 --> 00:00:42,170 Specifically, we need to implement LoaderCallbacks, and 10 00:00:42,170 --> 00:00:47,487 make sure we're picking the option from the support library. 11 00:00:47,487 --> 00:00:50,160 And let's use a bitmap array for our type parameter again. 12 00:00:53,040 --> 00:00:56,115 Then let's use Alt+Enter to implement the three required methods. 13 00:01:00,473 --> 00:01:03,760 The first method is onCreateLoader. 14 00:01:03,760 --> 00:01:08,530 In this method, we need to create a new instance of our loader and return it. 15 00:01:08,530 --> 00:01:11,179 So since our loader requires a URL as a parameter, 16 00:01:11,179 --> 00:01:14,722 let's first cut and paste the URL code into the top of this method. 17 00:01:23,957 --> 00:01:29,391 Then, right below the URL, let's return a new instance of our loader. 18 00:01:29,391 --> 00:01:33,911 return new MyAsyncTaskLoader, and 19 00:01:33,911 --> 00:01:40,188 passing in this for the context and url for the URL. 20 00:01:43,766 --> 00:01:46,614 I'd love to get rid of this return null as well, but 21 00:01:46,614 --> 00:01:51,121 even though our URL isn't malformed, we've still gotta catch the exception, 22 00:01:51,121 --> 00:01:54,620 which means we need to have a return down here. 23 00:01:54,620 --> 00:01:57,122 Well. Moving on to onLoadFinished, 24 00:01:57,122 --> 00:02:02,645 this method is called when your work is done and is where we should update the UI. 25 00:02:02,645 --> 00:02:08,440 So let's first update the data, our adapter is based on. 26 00:02:08,440 --> 00:02:13,552 myAdapter.bitmaps = data. 27 00:02:13,552 --> 00:02:18,091 And then let's notify our adapter that the data has been updated. 28 00:02:18,091 --> 00:02:21,971 myAdapter.notifyDataSetChanged. 29 00:02:21,971 --> 00:02:22,850 Nice. 30 00:02:22,850 --> 00:02:24,935 The last method is onLoaderReset. 31 00:02:26,370 --> 00:02:30,312 If your loader's been reset, then it no longer contains any data. 32 00:02:30,312 --> 00:02:33,167 So if you've got any references to data in your loader, 33 00:02:33,167 --> 00:02:36,130 you'd want to clean those up in this method. 34 00:02:36,130 --> 00:02:39,840 Since we don't have any way to reset our loader, we're fine leaving this empty. 35 00:02:41,060 --> 00:02:43,380 Now that we've implemented our loader callbacks, 36 00:02:43,380 --> 00:02:45,730 all that's left is to start our loader. 37 00:02:45,730 --> 00:02:46,831 At the bottom of onCreate. 38 00:02:51,888 --> 00:03:01,558 Let's call getSupportLoaderManager().initLoader(). 39 00:03:01,558 --> 00:03:05,850 Then let's pass in an arbitrary number for the ID, I'll choose 1. 40 00:03:07,025 --> 00:03:11,240 And an arguments bundle, which we don't have, so let's just pass in null. 41 00:03:12,570 --> 00:03:14,628 And finally a LoaderCallbacks object, 42 00:03:14,628 --> 00:03:17,920 which we've implemented right here in this activity. 43 00:03:17,920 --> 00:03:19,260 So let's just pass in this. 44 00:03:22,062 --> 00:03:26,684 And before we forget, let's make sure to show our progress bar while we're loading 45 00:03:26,684 --> 00:03:28,326 and hide it once we're done. 46 00:03:28,326 --> 00:03:32,388 So at the top of OnCreateLoader, 47 00:03:32,388 --> 00:03:39,938 let's add progressBar.setVisibility to View.VISIBLE. 48 00:03:39,938 --> 00:03:45,975 And then at the top of onLoadFinished, Let's set it to gone. 49 00:03:45,975 --> 00:03:55,650 progressBar.setVisibility(View.GONE); All right, time for the moment of truth. 50 00:03:55,650 --> 00:03:57,462 Let's run the app and see what happens. 51 00:04:02,483 --> 00:04:06,632 There's our progress bar, and now we've got our images. 52 00:04:06,632 --> 00:04:13,770 And if we rotate the app, Awesome, it's the same images! 53 00:04:13,770 --> 00:04:18,640 So instead of re-downloading all of those images, we just used our cached images and 54 00:04:18,640 --> 00:04:20,470 made for a much better user experience. 55 00:04:22,568 --> 00:04:24,610 When you've gotta do something in the background and 56 00:04:24,610 --> 00:04:28,040 you're worried about the activity lifecycle, just use a loader. 57 00:04:28,040 --> 00:04:30,880 Since a loader will stay alive through a rotation, it's a great 58 00:04:30,880 --> 00:04:35,880 place to do your loading and keep any data safe from being destroyed in an activity. 59 00:04:35,880 --> 00:04:39,440 If you'd like to learn more about loaders, check out the teacher's notes below. 60 00:04:39,440 --> 00:04:42,951 And as always, if you've got any questions or would like to try your hand at 61 00:04:42,951 --> 00:04:46,492 answering someone else's question, be sure to check out the community. 62 00:04:46,492 --> 00:04:47,440 Until next time.