Shared ViewModel - Explained | Android Studio Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
uh hello there and welcome back to my new video in this video i'm going to explain you more about share the view model and also show you how to use it inside your project so first let's start with the basics what is this so-called shared view model you might be thinking that we are talking about some new class called the shared view model well i must disappoint you because a shared view model is nothing but a regular viewmodel class which is sharing its instance with multiple fragments and if you are not familiar with vue models i highly suggest you to watch my video tutorial where i have explained everything you need to know about it so far you might got used on having a single viewmodel instance per fragment so when you regularly instantiate one viewmodel in your fragment it's often scoped to a life cycle of your fragment right and by scoping that one view model to a single fragment you cannot share values between multiple fragments which kind of makes sense now for example if you want to save a simple value in a local variable inside your regular view model which is scoped to a fragment a then you wouldn't be able to retrieve that a saved value from fragment b for example and in order to share values from the same viewmodel instance between the multiple fragments you need to scope a viewmodel to an activity instead so why inactivity you might be asking well it's because if you're using a navigation component in your project you are probably hosting all your fragments inside a single activity uh that way all your fragments will be scoped to an activity life cycle or in other words your fragments will live as long as your activity lives with that in mind we will be able to share a single viewmodel instance between a multiple fragments and that way use a sharedviewmodel soundsimple well it actually is and now enough theory let's see some practical examples okay so this is my simple project which i have already made and here i have a single activity and two fragments so as you can see both of those fragments are almost the same and they have a one edit text and the one button and what we are trying to achieve here uh basically we want to be able to save and read one variable from our view model in both of those fragments so uh for example if i save one country name in a view model from our first fragment we want to be able to read that same value from our second fragment as well and vice versa so now let's create one simple viewmodel class and i'm going to name this viewmodel a shared viewmodel so this class should inherit from a viewmodel of course and here i just want to create two variables in the one function so this first one will be named underscore country and i'm going to use a mutable live data of a type string of course and here we can set the default value for our country and i'm going to say here serbia for example okay so we can remove this string from here because we have already defined the default value and the type is uh omitted and here i can write a public variable which will be a read only variable so i'm going to name this variable country and the type should be a live data so a read only of a type string of course and we're going to set the value of our country from this underscore country so here we are having a backing property and we want to expose only this second variable a read-only variable to our fragments and this mutable live data should be available only inside our view model okay and down below i want to create a new function for example save a country and this function will be public as well so here is a parameter i want to add a new country of a type string okay and down below i want to set the value of our mutable live data or underscore country to the value from the parameters of this function so new country and that way we are going to only expose this public read-only variable and not this first one so now let me just open this main activity so you can see how our activity looks like so here our activity is just a basic and simple we have this setup action bar with nav controller so we can navigate back from our second fragment to our first fragment and let's open up our first fragment so as you can see this is how our first fragment looks like so we have a one edit text and one button and here basically what i want to do i want to initialize first view model so private val shared view model and here i'm going to use a buy keyword so i can delegate this viewmodel initialization to a delegate called the activity view models so you are already familiar with uh view models delegate so uh let me just add here a type so you are already familiar with delegate called the view models but this one is activity view models and this delegate is basically scoping our shared viewmodel instance to activity instead of this fragment so now our sharedviewmodel is a scope to our main activity and not just our first fragment and later from our second fragment we're going to also scope this uh shared view model to activity so we can share the values between those two fragments and now first let me just call this share view models and let's just observe this country uh live data object so let's observe here let me just pass a view lifecycle owner and here let's observe the country here i can just call a country and here i can call binding.first edittext dot set text and here i can call a country so basically whenever we open our application we want to get a text from our live data from a view model and set that value to our edit text and down below i want to call a binding dot first button and here i want to add on a click listener so whenever we click on our button i want to call a shared view model then a save country and here i want to get a text from our first edit text i want to convert that to string and i want to save that value to our shared view model and of course after that i want to navigate so from action first fragment to a second fragment okay and let me just open this application so you can see how our application will look like now so now uh when we run our application uh you will see that our edit text here is populated with this uh country that says serbia and that's the fault value which we have already specified inside our shared view model so as you can see right here and now basically whenever i change the value here and press a save button then we're going to save a new value to our shared view model but before i continue using this application i just want to basically copy the same thing to our second fragment so i'm going to just copy those lines of code and here in our second fragment i also want to initialize this viewmodel so sharedviewmodel by activityviewmodels okay because we want to scope this sharedviewmodel2activity so we can share the values between uh those first and second fragment and here inside i'm going to paste the same code from our first fragment and just change here those views so a second edit text here a second button and here a second edit text as well and here let's change this action so action second fragment to first fragment so now let me just open this application so we can see how our shared view model will work so now as you can see default value is a serbia which we have inside our shared view model and now let's try and change this for example to india and now when i press save this india value will be saved inside our share view model and we're going to get navigated to our second fragment and our second fragment will again show this value from our shared view model so click save and as you can see now we are inside our second fragment and this second fragment is displaying that same value from our shared v model so what does this actually means well it means that inside our first fragment we have successfully saved this country to our shared view model and we have successfully read that the same value to our second fragment and that's basically how our shared view model is working so basically we are sharing the instance of this same view model between multiple fragments and this shared view model is a scope to our main activity so of course if you don't want to use this activity view models delegate you can also initialize this view model in an old way so for example we can just comment this out and here i can write for example let me see a private late init bar a shared view model and down below we can also call this shared view model then we can use a view model provider and here for example we can pass this then we can call a get and here we can call our sharedviewmodel class so now let's see what will happen if we run our application so here we have a first fragment and the default value here is a serbia so now let's rename this serbia to india and let's save this value to our shared view model so click save and now as you can see inside our second fragment this value has not been changed and why is that well it's because that those fragments using a different instance of our share view model and in our second fragment we can see that this shared view model is not scoped to activity instead it's scoped to a fragment and if we want to scope this view model in this old way to activity we can uh here pass a require activity instead of this because require activity is actually pointing to our main activity and now if we run our application uh everything will work just fine as before so here let's change this to india for example okay click save and now as you can see that shared view model is sharing its instance between multiple fragments as before so now you can see that there are two ways of instantiating this view model so you can do that in old way but you need to be careful and here in a viewmodel provider you need to pass require activity instead of this if you want to share this viewmodel instance of course but me personally i prefer the first way of using this activity view models delegate and as you can see our application is working perfectly fine and we are successfully sharing our shared viewmodel instance between multiple fragments so that will be all for this video i hope you enjoyed please like this video if you find it helpful of course and see you next time
Info
Channel: Stevdza-San
Views: 15,792
Rating: undefined out of 5
Keywords:
Id: THt9QISnIMQ
Channel Id: undefined
Length: 11min 39sec (699 seconds)
Published: Mon Mar 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.