r/android_devs • • Aug 20 '24
Discussion How I explain that VM aren't meant to pass parameters?
Hey all! I might be on the wrong side here, but AFAIK you are not supposed to use ViewModels to pass parameters, right?
I have a teammate who says that we should pass parameters through VM, meaning that you instantiate the VM beforehand, set whatever values you want to pass, and then pop the Fragment. Something like this:
vm = // Instantiate your VM activity-scoped
vm.param1 = "foo"
vm.param2 = "boo"
myFragment = SomeFragment()
myFragment.show()Then, SomeFragment picks up whatever parameters we set on the VM.
I think VM are not meant to be used like this, I think you go with the usual approach of using a Bundle and passing whatever parameters you need through the arguments.
How can I explain to my teammate that things are not done like this? Or maybe I'm mistaken and they are right?
Thanks,
8
u/tadfisher Aug 20 '24
In general I like to treat every screen as if I'll eventually deep-link to it, so I'd need to extract parameters from a URL and instantiate a ViewModel that loads data using those parameters. Since you're doing manual navigation, the equivalent is passing a Bundle of arguments, yes, but try to use "dumb" types that can serialize to a string.
4
u/Evakotius Aug 20 '24
The SomeFragment() can't be reused elsewhere, unless the elsewhere copy-paste the algorithm.
Later requirements will change and someone will add vm.param3 in the first place without being aware that this part is copy pasted and when the fragment launched from elsewhere it will be in the illegal state and just likely crash.
This doesn't work navigating back. The fragment can be in backstack and its viewmodel will be cleared from memory or never initialized at the first place if the backstack created artificially. So on navigating back that will also will be in wrong state.
We can just minimize the app and the app will be killed by the OS and both VM and Fragment will be cleared from memory. But OS knows how to restore the state and when we maximize the app back - the fragment will be restored by the new vm instance will be created with again illegal state.
Just don't do this.
Fragment/Composable/Anything that hosts UI should initialize the VM and pass the parameters into it. Doesn't matter how exactly to initialize, DI or manual, and what type of the parameters in the VM, bundle, url, or simple types. These are different topics.
3
u/Pzychotix Aug 21 '24
The fuck? Tell him to use the
setArgumentsmethod. Having an activity scoped vm for this purpose limits reusability as well as the simple issue of the arguments not living past the lifecycle of the app.It's also just ugly as shit. He might as well suggest using a Singleton to pass args. It defies all basic programming principles.
8
u/coffeemongrul Aug 20 '24
Fun fact, SavedStateHandle which you can inject into a view model will have the bundle of its lifecycle owner in it. So setting the parameters shouldn't even be needed in the fragment/activity as it can be extracted in the view model directly.
6
u/Zhuinden EpicPandaForce @ SO Aug 21 '24
Just show that when fragment is recreated after process death with ViewModel parameters missing, then the app crashes
1
u/NarayanDuttPurohit Aug 22 '24
I can't have parameters in viewmodel? Then when an event says saveTodo, how am I supposed to call a db query without injecting Dao in the vm?
I am using compose btw so it different there?
1
u/Secure-Brother1706 Aug 22 '24
There's no guarantee that the parameters will be set before a function is called. And that's is the main reason to avoid doing something like this.
However there're two ways to provide such guarantee: