This course will be retired on July 14, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Data Persistence with Room!
You have completed Data Persistence with Room!
Preview
In this video we'll find 3 issues and talk about how we can fix them!
Code
implementation 'android.arch.lifecycle:extensions:1.1.1'
Related Links
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Now that we've got a basic
understanding of how the app works,
0:00
it's time to start solving problems.
0:03
Let's take a look at the app and
figure out what needs to be done.
0:05
Let's start by going into
the creator activity.
0:09
Let's add some toppings, Give our pizza
0:13
a name, And then rotate the screen.
0:18
And as we've seen before,
this is an issue.
0:26
Let's call this problem number one.
0:29
Creator activity does not save and
restore state after a rotation.
0:31
Typically, you'd solve this by
storing the state and bundle and
0:36
overwriting the activities
onSave InstanceState method.
0:40
But with the view model,
things have gotten easier.
0:43
Though technically, you should still
save state the old way as well,
0:46
more on that in the teacher's notes.
0:50
Getting back to the app,
if we add some toppings and
0:52
try to save the pizza, nothing happens.
0:56
So problem number two is,
pizzas aren't being saved.
1:00
In addition to the save
button not working,
1:04
the delete button also doesn't work.
1:06
But it's kind of hard to show
deleting a pizza without first
1:08
being able to save a pizza.
1:11
So really there's three
problems we need to solve.
1:13
CreatorActivity doesn't save and
restore state after a rotation.
1:16
Pizzas aren't being saved,
and pizzas can't be deleted.
1:20
Let's dive into problem number one with
the help of a new tool called ViewModel.
1:23
In Android,
1:28
a ViewModel is just a special object
that can survive a rotating activity.
1:29
In fact, if you look in the docs,
1:34
you'll find this chart showing
the life cycle of a ViewModel.
1:36
It gets created with the activity and
1:41
then stays alive until
the activity is actually finished.
1:43
So all we need to do to save state,
is store it in a ViewModel and
1:47
have the UI look to the ViewModel
to determine what to draw.
1:52
Back in the code, let's create a new
class called CreatorViewModel.
1:56
And let's make it extend
from the ViewModel class.
2:13
However, there's an import we should add
before we start using the ViewModel.
2:19
Let's go over to our build.gradle file.
2:24
Let me close off this.
2:27
And add in a line from
the teacher's notes below.
2:31
Then let's sync the project.
2:39
And get back to extending from ViewModel.
2:51
Let's use Alt+Enter to add the constructor
invocation, and we've got our ViewModel.
2:55
Now, we just need to add some variables to
store the state of our creator activity.
3:01
The state of the activity is
the name of the pizza and
3:07
the state of all the topping switches.
3:10
To store the name of the pizza,
let's add a variable called pizzaName,
3:13
and start it off with
a value of New Pizza.
3:19
Also, since the name of the pizza can
change, notice that we're using var,
3:23
instead of val.
3:28
To store the state of
the topping switches,
3:30
we'll use a mutable map
of toppings to Booleans.
3:32
Let's type val and
call it switch states, and
3:36
set it equal to an empty mutableMap.
3:40
And for the types,
let's give it Topping and Boolean.
3:48
Look like we'll need to import
Topping with Alt+Enter.
3:53
It may seem weird that
we're able to use val here.
3:57
However, when we make changes to this map,
we'll just be adding and
4:00
removing key value pairs.
4:05
We'll never need to set switchStates
equal to anything else, so
4:07
we can get away with making it a val,
awesome.
4:12
Now that we've got our ViewModel,
let's see how to use it.
4:15
Over in creator activity, at the top,
4:18
let's add another lateinit
var named viewModel.
4:21
Let's give it a type of CreatorViewModel.
4:27
Then, down in on create,
4:31
let's add a line after
we get our PIZZA_ID and
4:34
set viewModel = ViewModelProviders.of,
4:40
then you pass in the fragment or activity.
4:45
So let's just go with this.
4:51
And now, we have a ViewModelProvider for
our activity.
4:54
To get the viewModel from here,
we just need to call the get function and
4:58
pass in the class of the viewModel,
5:04
CreatorViewModel::class.java.
5:07
And there we go, we've got our viewModel.
5:12
Now we need to do two things.
5:15
We need to use our ViewModel to
update the activity's UI and
5:18
we need to update the ViewModel
when any UI changes are made.
5:21
We'll tackle those issues
in the next video.
5:25
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up