in this video we're gonna have a look at
what the Android view model class is and
see how it fits into our apps
architecture now a good starting point
is the Google documentation which you've
got opened at this page it's the Android
arch lifecycle view model page so view
model is part of the new architecture
components so it's a lifecycle aware
class and we'll see that it saves a lot
of work and we have to do with the
activity lifecycle you know the
reference documentation mensen mentions
that so let's see what it has to say
specifically about view model so firstly
we have a look at it the first SAR
paragraph there the first sentence says
that it's responsible for preparing and
managing the data for an activity or
fragment you know we'll be using
fragments a bit later and it's good to
know that we can use a view model with
them as well as with an activity so next
it goes on to say that a view model is
always created in association with a
scope in other words a fragment or an
activity and it will be retained as long
as the scope is alive personally I think
that gives the wrong impression and
implies that the view model will be
destroyed when the activity is but the
next paragraph may be down still further
clarifies that and actually says this
means that a view model will not be
destroyed if its owner is destroyed for
a configuration change so all the state
that we've had to concern ourselves with
saving will be retained if we put it
into a view model because the view model
survives a configuration change nothing
will be lost so that's good we'll see
how that really simplifies things when
we use one in our apps interestingly for
the new instance of the owner we'll just
reconnect to the existing view model so
we don't have to worry about
reconnecting our activity if it's
destroyed as a result of a configuration
change Android takes care of all of that
for us now we're going to come back to
the next paragraph in a minute but the
one after that makes it really clear
what the role of view model is basically
comes out goes under here to save you
models only responsibility is to manage
data for the UI that's clear enough and
the documentation is quite emphatic that
it should never access your view
hierarchy or hold a reference back to
the activity or a fragment okay so that
seems to be a problem if we can't hold a
reference to the activity how can we
call it back when data changes well the
answer is actually in that paragraph
that I skipped rather than the view
model calling a function in the activity
the activity observes the view model the
view model exposes its data and an
observable can monitor it the Android
solutions are live data which we'll be
using in the calculator app or Android
data binding
now when Google designed those
architecture components they made sure
that they would work with other
frameworks such as rx Java so if you end
up using a framework like that things
will still work and I think that's good
to know
all right so scrolling down and we can
see down here that the class really
isn't huge at all it's got a single
parameter less constructor and one
function on cleared anything that we
need the view model to do we write
ourselves the basic class itself is very
simple now the uncleared function is
called when the view model is no longer
required so that we can clean up any
resources that our view models using now
before I move on I'm just going to check
out this link over here to the left-hand
side for what view a model provider
there's quite a little bit to see that
click on view model provider so this is
a utility class that provides view
models for a scope so the scope that
it's talking about is our activity or
fragment when we come to use one of
those so we don't create our own
instances of a view model we use the
view model provider to create it for us
and we'll see that in action when we
convert the calculator app to use a view
model and we'll start work on that in
the next video