How to Share Data between Fragment and Activity || Share Data using ViewModel || FoxAndroid || 2021

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there everyone welcome to another video fox android so in this video we're going to learn how to share data between the fragments and the host activity using shared view model right so what does that means that if you have an activity that hosts various fragments and you want to share the data between the activity and those fragments using view model right shared view model then how to implement that so in case you do not know what are fragments how to implement them i already have a video on that you can just check out that video before going further by clicking on this i button so let me just give you a brief demonstration of what you're gonna get by the end of this video so if you can see on my mobile screen i have this activity and inside this on the upper half i have one fragment one and if i just change it i have another fragment so basically this activity hosts two fragments now i want to send data from these fragments to the activity right so if you can see inside this fragment one we have this edit text so if i just put anything like uh sent so for instance i have a written send data to activity if i just tap on this button you can see here the textview inside the activity has been changed so that data has been sent to this activity and similarly we can send data from fragment2 as well so we'll be learning that if you want to implement the same to your android application make sure you watch this video till the end so without much of let's get started [Music] so guys if you're new to this channel make sure to hit that subscribe button and press the bell notification icon for saving the notification of the upcoming videos and by the end of the video if you like the video make sure to hit that thumbs up button as well so before uh going for the coding part let me just demonstrate that how the things gonna work so if i just come here so you can see we have a view model here right this view model will be uh will act as a center authority that will be responsible for sharing data between the fragments and the activity so we have fragment one and fragment two right fragmented and fragment b so these fragment will write the data to the view model right and similarly the fragment we will write the data to this view model as well the main activity have a listener on this view model what does a listener means that any if there is any change in the view model the data of the view model right this live listener will be triggered a callback function will be called right then the main activity will know that there is some change in the data so it will listen to that change and do the required actions in the main activity so this is how things gonna work now let us just go to our android studio so uh we already have implementation of fragments right so if you want to see this you can check out the previous video of this playlist now let us just do some modification in here so for example now this uh fragment thing is occupying a lot of space so let us just make it 400 maybe yeah perfect and we need to do some changes in here as well so for instance the margin top for this will be 200 dp and similarly we'll do that for fragment 2 as well right and one more thing we are going to do is we are going to implement a text view inside this main activity to show the data that we are receiving from the fragments okay we have uh got this text view inside our main activity basically we'll be changing this text view right whenever we are saving the data from the fragments now let us just go to the back-end code so uh the very first thing we are going to do is we are going to create a new class for the view model so let me just right click here java class so let me just name it as item view model right now this class will extend don't worry about this pop-up it is just that i have hosted this project on github that's why we're get getting this pop-up right so okay this class needs to extend view model so that will make it a viewmodel class right now we are going to declare a variable so okay we have created the object of mutable live data class so what does this mutable live data means that means uh this particular item can be changed right mutable and this is a live data that means any change in this will trigger a callback function that will let us know that some data has been changed inside this object right the type of this is string you can uh just modify it according to your application right for this tutorial sake i am taking the type as string here so after that we are going to create a method set data right this method will be used to set the data inside this so it will take one argument that is a string right the data we want to write so we'll pass item here and the another method [Music] so this particular method will be called uh to get the item from this viewmodel right so it will return the live data and type of which will be string so that is pretty much everything we need to do inside this viewmodel class right now we'll just go to our main activity so here we want to receive the data right so the very first thing we are going to do is we are going to refer to the text view that we just created and we are going to create a reference table for our viewmodel class so item view model so let us just write the logic for saving the data so okay we have got the instance of this viewmodel class right then we have attached the observer on this right so this is the way we attach observer to a viewmodel class now this this particular method will be called once there is a change in the data any change in the data this method will be invoked inside main activity now what we want to do when this particular method is called we want to change this particular text view right so we'll do that right we have set the text now we'll just go going to implement this logic in fragment one that means we should write the data to this new model class whenever the user taps on this fragment button the button that is there in the fragment so when user taps on this the data should be returned to the viewmodel class right so for that yes we are going to override the method here on view created right and let us just let us just comment this out because we are not going to use the result api so let us just declare a reference variable for viewmodel class right and first we are going to create a instance of it and so okay guys we have just got the instance of this viewmodel class right this particular viewmodel class then we have referred to the button that is there inside the fragment then we set the on click listener so once the user tap on that button will call this particular method set data that will set the data inside this right selected item so we have called this method here viewmodel.set data and we have passed the data that has been typed inside the string right and similar thing we need to do inside the fragment 2 class so let me just copy this to save some time so if i just copy it and let me just paste it here and let us just comment this thing out till here right if if you're just watching this tutorial the first time you might not have this code right then you can do this and let me copy this as well so here we have to change two things we have refer to the second button and here is it right so we have done similar thing with fragment 2 as well we have refer to the button of the second fragment and we have got the data of the second edittext right so that is pretty much everything we need to do to implement this feature now let me just quickly install this app to my cell phone and check if it is working fine or not right so guys the app has been installed to my cell phone as you can see on my mobile screen now if i just type in some data here okay if i just tap on this button you can see we have got the data inside this activity right if we just go to fragment2 type something here if i just tap on this you can see we are observing the data in real time so i guess we have successfully implemented this feature to our android application and i also hope that you guys have understood the code in case of any route you can always ask me in the comment section or you can dm me on instagram the instagram username will be there on screen right now and if you like the video make sure to hit that thumbs up button and in case you're into this channel make sure to hit that subscribe button and press the bell notification icon for receiving the notification of the upcoming videos so that is it for today's video to see another video bye [Music] you
Info
Channel: Foxandroid
Views: 25,509
Rating: undefined out of 5
Keywords: fragment, android studio, share data, share data between fragment and activity, viewmodel, share data using viewmodel, shared viewmodel, share data between fragment and activity using viewmodel, communication between activity and fragment using viewmodel, fragment viewmodel, fragment viewmodel example, fragment viewmodelproviders, android viewmodelproviders fragment, android pass viewmodel to fragment, viewmodel fragment android, android shared viewmodel fragment, 2021, foxandroid
Id: H_ItzJp5yVE
Channel Id: undefined
Length: 12min 19sec (739 seconds)
Published: Sun Jun 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.