1 00:00:04,640 --> 00:00:05,920 Welcome back. 2 00:00:05,920 --> 00:00:12,420 In this video, we've got some slides to explain how different instances of the same ViewModel work. 3 00:02:07,060 --> 00:02:10,880 Before making the change, we can confirm that behaviour. 4 00:02:11,160 --> 00:02:12,360 I'll run the app again. 5 00:02:20,620 --> 00:02:22,620 and filter the logcat 6 00:02:24,060 --> 00:02:26,060 on tasktimerviewmodel 7 00:02:28,760 --> 00:02:38,519 to make it easier to find things, use control f and search tasktimerviewmodel 8 00:02:38,520 --> 00:02:43,420 That highlights the entries to distinguish them from the app provider entries. 9 00:02:43,420 --> 00:02:46,920 Tap the plus button to add a new task task 10 00:02:46,920 --> 00:02:49,900 Task 9 11 00:02:50,380 --> 00:02:53,480 9th task. 12 00:02:55,100 --> 00:03:01,180 Save the task and we can see a new TaskTimerViewModel instance 13 00:03:01,580 --> 00:03:04,240 created in the logcat. 14 00:03:04,640 --> 00:03:08,000 We can see the entries TaskTimerViewModel created 15 00:03:08,160 --> 00:03:09,700 appearing twice 16 00:03:11,420 --> 00:03:15,660 and each one is accompanied by AppProviderQuery called with 17 00:03:16,020 --> 00:03:17,840 and the URI. 18 00:03:18,000 --> 00:03:25,900 We've already seen the change we need to make in AddEditFragment. 19 00:03:27,520 --> 00:03:33,100 We just need to change this to activity 20 00:03:35,680 --> 00:03:41,520 We've had to use the bangbang operator there, to provide a non nullable activity 21 00:03:41,530 --> 00:03:47,130 As we know from the fragment life cycle the activity can be null at times. 22 00:03:47,130 --> 00:03:52,510 As a result, it is essential that we don't attempt to use the view model any 23 00:03:52,510 --> 00:03:55,200 earlier than onActivityCreated. 24 00:03:55,200 --> 00:03:58,300 A lazy delegate will initialize view model the 25 00:03:58,400 --> 00:04:03,480 first time it is referred to, and the activity is non null by the time 26 00:04:03,480 --> 00:04:09,140 onActivityCreated runs. I'll run the app again 27 00:04:12,720 --> 00:04:14,720 and keep an eye on logcat. 28 00:04:16,300 --> 00:04:21,079 I've still got the filter, to make it easier to see just the bits that we're interested in. 29 00:04:21,100 --> 00:04:23,760 When MainActivityFragment starts, 30 00:04:24,900 --> 00:04:28,360 we get a TaskTimerViewModel instance created, 31 00:04:28,700 --> 00:04:30,700 and the database is queried 32 00:04:30,700 --> 00:04:32,700 I'll edit task 10 33 00:04:33,120 --> 00:04:43,800 and change the word first back to the digit 1st 34 00:04:45,360 --> 00:04:49,440 when I tap save, we no longer get a new ViewModel instance, 35 00:04:49,680 --> 00:04:53,340 and the database is only requeried after the update. 36 00:04:53,820 --> 00:04:56,040 Okay, that's the end of this video. 37 00:04:56,640 --> 00:04:59,800 We've gone into a lot more detail about ViewModels, 38 00:04:59,800 --> 00:05:01,800 but I think it's important to understand 39 00:05:01,800 --> 00:05:05,040 the things we've covered here, if you're going to use them without getting 40 00:05:05,040 --> 00:05:06,260 strange bugs. 41 00:05:06,920 --> 00:05:09,340 The scope that you use for the view model is important, 42 00:05:09,620 --> 00:05:12,280 especially when using them for multiple fragments. 43 00:05:12,440 --> 00:05:14,960 In this example, and the slides, 44 00:05:15,100 --> 00:05:19,980 I've explained what's going on and why you may want to use the Activity as the 45 00:05:19,980 --> 00:05:23,700 scope, when you have more than one Fragment at the same time. 46 00:05:24,120 --> 00:05:24,880 Our app can now 47 00:05:24,880 --> 00:05:27,880 add, edit and delete the task records. 48 00:05:27,880 --> 00:05:31,660 I'm going to stop this section here but that's not the end of our app. 49 00:05:31,860 --> 00:05:34,480 In the next section we'll use it to have a look 50 00:05:34,480 --> 00:05:39,000 at dialogues. See you in the next section