So what do you all think about having reference to...
# android-architecture
a
So what do you all think about having reference to view in xml with databinding, like creating a variable of type Fragment or Activity and then setting it into binding to invoke methods directly on click for example
j
That would be interesting to see, actually.
a
There is nothing complicated 🤷 just create a variable in the xml with the type of your Fragment for example and in the Fragment itself set it through like so for example binding.fragment = this
Just wondering if it is something smelly here
I personally use only viewmodel as a variable and never view
s
XML is view. Activity or Fragments have a reference to XML via binding. So actually the other way around. Do you know why you use a viewmodel as a data provider reference in the XML ?
a
Because view model is a holder for observable fields so you can update them directly according to your presentation logic from viewmodel and then they are going to be picked up by UI
r
I don't think its a good idea. The variable type shouldn't be a activity or fragment. I think making the variable type a viewmodel is better because its already lifecycle .
a
I also feel so but they question is ...why? There is no mentioning of this online as far as I could find @rkeazor
r
So databinding, is for reflecting data in your xml. In a good android architecture, you normally separate your data layer from your activity and fragment. Futhermore android context is available by default in databinding, so there is no real reason to bind your whole activity or fragment.
a
The exact use case was navigation. If you just need to open a new fragment for example on click why going through the viewmodel if you can cut the corner and just call fragment
Also to the point of the separation then view model can't be in xml
As xml and fragment/activity are all part of the view layer as it is and adding viewmodel will add logic layer bleeding into the view which is the main concern I hear about the databinding
r
sorry was eating breakfast lol . So no, the viewModel isn't cutting layers. Before Databinding people would still seperate there activity and fragment from there logical layer(hence viewmodels and presenters). However prior to databinding they also would alot of random 3 line setter methods . like setVisbility(visble: Boolean) setText setenabled ect... This way they were able to set the view with the data they got from there logical layer
But databinding removes the need for the 3 line setter methods
a
Before databinding (or observer pattern) I feel people did more in activity and fragment anyways with having callback to UI anyways. So the gist is you feel that the navigation use case still should go through the viewmodel even though navigation needs context to access nav controller?
r
Yea but does dev who did alot in the activity or fragment where just writing bad architected code lol. The more logic you have in your activity or fragment, the worst testing becomes, and the more unreadable it becomes
sure, why not.if thats the setup for it
I havent used navigation component with viewmodel
your activity context cannot be in a viewmodel
only your applicationcontext
viewmodel
viewmodel +activityContext = memoryLeak
a
That's the point , navigation requires fragment or activity to get nav host fragment
So it is gonna look like a command push from view model to view to execute navigation
Binding - view model - view - navigation
And I was asked it it is ok to go binging - view - navigation
r
I am alittle bit lost. I just went thru a example of navgraph with viewmodel and I didnt see anything about passing the activity or fragment to the navgraph .https://medium.com/androiddevelopers/viewmodels-with-saved-state-jetpack-navigation-data-binding-and-coroutines-df476b78144e
can you perhaps demonstrate with some code what you mean
a
To use navigation you have to access NavHostFragment so your method will look like this findNavHostFragment(). navigate (///) That article talks about having view models scoped within navigation pattern
And also findNavHostFragment is an extension function to Fragment