G'day everyone, welcome back.
Now that we've created our TaskTimerViewModel class,
we need to subscribe to the ViewModel.
We do that in MainActivityFragment.
We create a ViewModel instance first, as we saw in the previous section
and we've got an error.
ViewModelProviders isn't recognized.
Your first mini-challenge is to work out why and to fix the problem.
We did discuss this back in the previous section, so refer to that, if you can't work out why there's an error.
Pause the video now and we'll continue in a few minutes.
Did you work out what was wrong?
We haven't added the dependency for the ViewModel and LiveData libraries to our build.gradle file.
Let's have a look.
Let's add in our missing code
and then click sync now.
This is also a good time to review the versions, to see if any of the components have been updated.
Ok, we've got a ViewModel instance.
Next, we need to subscribe to it. I'll do that in the onCreate function.
Choose the Android.arch.lifecycle.observer import, if you're prompted.
This is where we'll call our adapter's swapCursor function, to provide the new cursor, when we observe that it has changed.
When the cursor changes, we pass the new one to swapCursor,
causing the adapter to get the new data.
We wrote swapCursor to return the old cursor, allowing us to call its close function here.
That takes care of freeing up the cursor resources.
We use a safe core, in case the return cursor is null.
We can now run the app and we should see our tasks appearing in the list.
That's looking good but it's still not quite finished.
To see why, watch what happens when I add a new task.
We're up to task 5, so I'm going to add tasks 6,
and we'll give it the sort order of 6.
When I tap on the Save button, we return to the task list but the new task doesn't appear.
It has been successfully saved, as we can see if I close the app and then run it again.
We'll look at how to fix that in the next video. See you then.