1 00:00:04,580 --> 00:00:06,380 G'day everyone. Welcome back. 2 00:00:06,380 --> 00:00:11,240 Our adapter's now set up to call our fragment, when something interesting happens, 3 00:00:11,240 --> 00:00:13,120 such as the user tapping a button. 4 00:00:13,120 --> 00:00:17,680 In this video, we'll get the fragment to respond to the callbacks. 5 00:00:17,680 --> 00:00:20,560 Looking at our CursorRecyclerViewAdapter, 6 00:00:20,560 --> 00:00:26,500 we can see that it's expecting a listener that implements the onClickListener interface. 7 00:00:26,500 --> 00:00:38,260 The first step then, is to get the MainActivityFragment to implement that interface. 8 00:00:38,260 --> 00:00:43,960 We've got an error, as usual, but that's because we haven't implemented the interface functions yet. 9 00:00:43,960 --> 00:00:51,080 We'll fix that in a moment. To begin with, there's another error when we create our CursorRecyclerViewAdapter 10 00:00:51,080 --> 00:00:59,680 We added an extra parameter, so now we have to provide a listener. 11 00:00:59,680 --> 00:01:05,620 Because MainActivityFragment implements the onTaskClickListener interface, 12 00:01:05,620 --> 00:01:10,600 we can pass the current instance of it as the listener for the adapter. 13 00:01:10,600 --> 00:01:16,640 That means our MainActivityFragment will be called back by the adapter, when a button's tapped. 14 00:01:16,640 --> 00:01:19,880 As a reminder, that's why we use an interface. 15 00:01:19,880 --> 00:01:25,200 The Kotlin compiler won't compile any class that says that it implements an interface, 16 00:01:25,200 --> 00:01:29,380 but that then doesn't provide all the functions that the interface defines. 17 00:01:29,380 --> 00:01:34,580 The interface makes sure that any object we pass as the listener to our adapter, 18 00:01:34,580 --> 00:01:39,140 definitely does have all the functions that the adapter's going to call. 19 00:01:39,140 --> 00:01:47,660 Okay, we'll get Android Studio to generate the interface functions, after onActivityCreated. 20 00:01:47,660 --> 00:01:57,800 Control I is used to generate the functions that we need to implement, and we just select them all from the dialog. 21 00:01:57,800 --> 00:02:03,920 I won't do this in the next bit of the video, but if you want to add logging to those functions, and run the app, 22 00:02:03,920 --> 00:02:07,660 you'll see them being called when you tap the buttons. 23 00:02:07,660 --> 00:02:13,460 So that's the easy bit done. Next we have to decide what these functions are going to do, 24 00:02:13,460 --> 00:02:15,460 and that requires a bit of thought. 25 00:02:15,460 --> 00:02:18,040 We'll start with the onEditClick. 26 00:02:18,040 --> 00:02:23,760 What the user will want to happen, is that they can edit the task when they tap the edit button. 27 00:02:23,760 --> 00:02:27,700 If you try to do that from here though, you'll end up in a real mess. 28 00:02:27,700 --> 00:02:32,840 A Ffragment shouldn't attempt to add another Fragment to its Activity's layout. 29 00:02:32,840 --> 00:02:37,820 Remember that it's the Activity that hosts the addEditFragment. 30 00:02:37,820 --> 00:02:44,660 The only sensible thing to do here is to call back to MainActivity and let it take care of things. 31 00:02:44,660 --> 00:02:51,500 That should be quite easy, because MainActivity is already doing everything it needs to do to edit the task. 32 00:02:51,500 --> 00:02:59,740 We haven't edited an existing task yet, but the taskAditRequest function will edit if it's given a non-null task. 33 00:02:59,740 --> 00:03:02,260 We're going to create a chain of callbacks. 34 00:03:02,260 --> 00:03:07,860 Our adapter will call the onEditClick function in the MainActivityFragment, 35 00:03:07,860 --> 00:03:12,940 and then MainActivityFragment will call another function in MainActivity. 36 00:03:12,940 --> 00:03:17,580 To do that, MainActivityFragment will define an interface, 37 00:03:17,580 --> 00:03:22,440 and MainActivity will implement it. And that sounds like a good challenge. 38 00:03:22,440 --> 00:03:26,100 Define an interface in MainActivityFragment. 39 00:03:26,100 --> 00:03:33,880 If you want to use the same name as me, I'll be calling mine OnTaskEdit, but you can call it anything you want. 40 00:03:33,880 --> 00:03:38,860 The interface should define a single function, which takes a task as an argument. 41 00:03:38,860 --> 00:03:46,760 Again, you can call it what you want, but I'll be calling mine onTaskEdit with a lowercase o. 42 00:03:46,760 --> 00:03:52,280 Change MainAactivity to implement the interface, then add the required function. 43 00:03:52,280 --> 00:03:59,180 When you've made the changes, run the app and test that tapping the edit button edits the correct task. 44 00:03:59,180 --> 00:04:05,600 If you have problems casting the activity to the interface type, check out the earlier video in this section, 45 00:04:05,600 --> 00:04:09,100 up and back navigation for fragments. 46 00:04:09,100 --> 00:04:16,760 In that video, we covered casting the activity to an AppCompatActivity, and discussed how to do that properly. 47 00:04:16,760 --> 00:04:20,920 In that example, we had a listener reference to the activity, 48 00:04:20,920 --> 00:04:23,320 but the same principle applies here. 49 00:04:23,320 --> 00:04:26,060 Just use activity rather than listener. 50 00:04:26,060 --> 00:04:31,160 I'll stop this video here and go over the solution in the next video. 51 00:04:31,160 --> 00:04:33,720 I'll see you there.