1 00:00:04,700 --> 00:00:06,890 in this video we're gonna have a look at 2 00:00:06,890 --> 00:00:09,680 what the Android view model class is and 3 00:00:09,680 --> 00:00:11,210 see how it fits into our apps 4 00:00:11,210 --> 00:00:13,610 architecture now a good starting point 5 00:00:13,610 --> 00:00:16,070 is the Google documentation which you've 6 00:00:16,070 --> 00:00:19,010 got opened at this page it's the Android 7 00:00:19,010 --> 00:00:22,640 arch lifecycle view model page so view 8 00:00:22,640 --> 00:00:24,550 model is part of the new architecture 9 00:00:24,550 --> 00:00:27,470 components so it's a lifecycle aware 10 00:00:27,470 --> 00:00:29,900 class and we'll see that it saves a lot 11 00:00:29,900 --> 00:00:31,369 of work and we have to do with the 12 00:00:31,369 --> 00:00:33,470 activity lifecycle you know the 13 00:00:33,470 --> 00:00:35,600 reference documentation mensen mentions 14 00:00:35,600 --> 00:00:37,519 that so let's see what it has to say 15 00:00:37,519 --> 00:00:39,890 specifically about view model so firstly 16 00:00:39,890 --> 00:00:42,080 we have a look at it the first SAR 17 00:00:42,080 --> 00:00:44,239 paragraph there the first sentence says 18 00:00:44,239 --> 00:00:46,670 that it's responsible for preparing and 19 00:00:46,670 --> 00:00:49,159 managing the data for an activity or 20 00:00:49,159 --> 00:00:50,540 fragment you know we'll be using 21 00:00:50,540 --> 00:00:52,790 fragments a bit later and it's good to 22 00:00:52,790 --> 00:00:54,260 know that we can use a view model with 23 00:00:54,260 --> 00:00:57,140 them as well as with an activity so next 24 00:00:57,140 --> 00:00:58,970 it goes on to say that a view model is 25 00:00:58,970 --> 00:01:01,970 always created in association with a 26 00:01:01,970 --> 00:01:03,949 scope in other words a fragment or an 27 00:01:03,949 --> 00:01:06,440 activity and it will be retained as long 28 00:01:06,440 --> 00:01:08,930 as the scope is alive personally I think 29 00:01:08,930 --> 00:01:10,609 that gives the wrong impression and 30 00:01:10,609 --> 00:01:12,680 implies that the view model will be 31 00:01:12,680 --> 00:01:16,250 destroyed when the activity is but the 32 00:01:16,250 --> 00:01:17,960 next paragraph may be down still further 33 00:01:17,960 --> 00:01:20,090 clarifies that and actually says this 34 00:01:20,090 --> 00:01:22,009 means that a view model will not be 35 00:01:22,009 --> 00:01:25,130 destroyed if its owner is destroyed for 36 00:01:25,130 --> 00:01:28,130 a configuration change so all the state 37 00:01:28,130 --> 00:01:29,750 that we've had to concern ourselves with 38 00:01:29,750 --> 00:01:32,960 saving will be retained if we put it 39 00:01:32,960 --> 00:01:35,179 into a view model because the view model 40 00:01:35,179 --> 00:01:37,789 survives a configuration change nothing 41 00:01:37,789 --> 00:01:40,310 will be lost so that's good we'll see 42 00:01:40,310 --> 00:01:42,170 how that really simplifies things when 43 00:01:42,170 --> 00:01:45,409 we use one in our apps interestingly for 44 00:01:45,409 --> 00:01:47,390 the new instance of the owner we'll just 45 00:01:47,390 --> 00:01:50,719 reconnect to the existing view model so 46 00:01:50,719 --> 00:01:51,590 we don't have to worry about 47 00:01:51,590 --> 00:01:53,899 reconnecting our activity if it's 48 00:01:53,899 --> 00:01:55,520 destroyed as a result of a configuration 49 00:01:55,520 --> 00:01:58,249 change Android takes care of all of that 50 00:01:58,249 --> 00:02:00,439 for us now we're going to come back to 51 00:02:00,439 --> 00:02:03,109 the next paragraph in a minute but the 52 00:02:03,109 --> 00:02:05,179 one after that makes it really clear 53 00:02:05,179 --> 00:02:08,270 what the role of view model is basically 54 00:02:08,270 --> 00:02:09,710 comes out goes under here to save you 55 00:02:09,710 --> 00:02:12,380 models only responsibility is to manage 56 00:02:12,380 --> 00:02:15,680 data for the UI that's clear enough and 57 00:02:15,680 --> 00:02:18,290 the documentation is quite emphatic that 58 00:02:18,290 --> 00:02:20,060 it should never access your view 59 00:02:20,060 --> 00:02:22,430 hierarchy or hold a reference back to 60 00:02:22,430 --> 00:02:25,549 the activity or a fragment okay so that 61 00:02:25,549 --> 00:02:27,920 seems to be a problem if we can't hold a 62 00:02:27,920 --> 00:02:30,109 reference to the activity how can we 63 00:02:30,109 --> 00:02:32,420 call it back when data changes well the 64 00:02:32,420 --> 00:02:34,280 answer is actually in that paragraph 65 00:02:34,280 --> 00:02:36,980 that I skipped rather than the view 66 00:02:36,980 --> 00:02:38,959 model calling a function in the activity 67 00:02:38,959 --> 00:02:42,140 the activity observes the view model the 68 00:02:42,140 --> 00:02:44,810 view model exposes its data and an 69 00:02:44,810 --> 00:02:47,840 observable can monitor it the Android 70 00:02:47,840 --> 00:02:50,000 solutions are live data which we'll be 71 00:02:50,000 --> 00:02:52,010 using in the calculator app or Android 72 00:02:52,010 --> 00:02:53,450 data binding 73 00:02:53,450 --> 00:02:55,040 now when Google designed those 74 00:02:55,040 --> 00:02:57,470 architecture components they made sure 75 00:02:57,470 --> 00:02:58,549 that they would work with other 76 00:02:58,549 --> 00:03:01,879 frameworks such as rx Java so if you end 77 00:03:01,879 --> 00:03:03,980 up using a framework like that things 78 00:03:03,980 --> 00:03:05,659 will still work and I think that's good 79 00:03:05,659 --> 00:03:05,959 to know 80 00:03:05,959 --> 00:03:10,370 all right so scrolling down and we can 81 00:03:10,370 --> 00:03:11,599 see down here that the class really 82 00:03:11,599 --> 00:03:13,939 isn't huge at all it's got a single 83 00:03:13,939 --> 00:03:15,919 parameter less constructor and one 84 00:03:15,919 --> 00:03:18,590 function on cleared anything that we 85 00:03:18,590 --> 00:03:20,450 need the view model to do we write 86 00:03:20,450 --> 00:03:22,940 ourselves the basic class itself is very 87 00:03:22,940 --> 00:03:25,430 simple now the uncleared function is 88 00:03:25,430 --> 00:03:27,470 called when the view model is no longer 89 00:03:27,470 --> 00:03:29,599 required so that we can clean up any 90 00:03:29,599 --> 00:03:33,379 resources that our view models using now 91 00:03:33,379 --> 00:03:34,669 before I move on I'm just going to check 92 00:03:34,669 --> 00:03:35,959 out this link over here to the left-hand 93 00:03:35,959 --> 00:03:39,319 side for what view a model provider 94 00:03:39,319 --> 00:03:40,760 there's quite a little bit to see that 95 00:03:40,760 --> 00:03:45,560 click on view model provider so this is 96 00:03:45,560 --> 00:03:48,139 a utility class that provides view 97 00:03:48,139 --> 00:03:50,720 models for a scope so the scope that 98 00:03:50,720 --> 00:03:52,699 it's talking about is our activity or 99 00:03:52,699 --> 00:03:54,620 fragment when we come to use one of 100 00:03:54,620 --> 00:03:56,540 those so we don't create our own 101 00:03:56,540 --> 00:03:59,000 instances of a view model we use the 102 00:03:59,000 --> 00:04:01,549 view model provider to create it for us 103 00:04:01,549 --> 00:04:03,799 and we'll see that in action when we 104 00:04:03,799 --> 00:04:06,049 convert the calculator app to use a view 105 00:04:06,049 --> 00:04:07,790 model and we'll start work on that in 106 00:04:07,790 --> 00:04:10,540 the next video