r/androiddev • u/Zhuinden • Feb 10 '20
Library New ViewBinding sample in the Architecture Components Samples github repo
https://github.com/android/architecture-components-samples/tree/9f021451fd64362c7c227802bacf8cfe476af0be/ViewBindingSample1
u/NahroT Feb 10 '20
Why imperatively mutate views when you can declaratively bind it with databinding.
1
u/Zhuinden Feb 11 '20
Because writing "imperative" view code (is it even imperative if you declare LiveData observers?) is easier than writing binding adapters and inverse binding adapters.
3
u/pavi2410 Fuchsia Dev Feb 10 '20
What does bind method do?
3
Feb 10 '20
Looks like the same as
inflatebut allows you to use the new pass your layout reference in the Fragment constructor way of doing things.3
u/pavi2410 Fuchsia Dev Feb 10 '20
Ah! So the Fragment constructor creates the view, and bind method just gives the view references instead of inflating the layout. Cool!
2
u/SkiFire13 Feb 10 '20
The constructor just saves the layout id to be used in the onCreateView when the view is created
1
Feb 10 '20
Note you need Android Studio 3.6+ for this, it's broken pretty badly for our medium sized project. The auto-generated classes failed for various custom views (sorry not kept the details, just did a hard reset).
0
u/druidhealereurope Feb 10 '20
I suppose this is useful if you're working with Java.
1
u/Zhuinden Feb 10 '20
Or if you want compile-time-safe view lookup instead of relying on the imports which may or may not throw an exception on access.
0
u/kakai248 Feb 10 '20
Unfortunately it's so ugly to use with fragments. The view lifecycle really ruins it.
7
u/Zhuinden Feb 10 '20 edited Feb 24 '20
Why, what's wrong with
class MyFragment: Fragment(R.layout.blah) { private val binding by autoCleared { FragmentMyBinding.bind(view) } }?
You could technically mess around a bit and make it
by viewBinding(FragmentMyBinding::bind)8
u/kakai248 Feb 10 '20
Wait, that's not that bad. They should really ship that delegate instead of advocate for nulling it in
onDestroyView7
u/Zhuinden Feb 10 '20
The trick is in GithubBrowserSample where they show-case how to create
AutoClearedValue: https://github.com/android/architecture-components-samples/blob/0c8fda35231f55bcc2bda080ce7415f39282a268/GithubBrowserSample/app/src/main/java/com/android/example/github/util/AutoClearedValue.kt#L62Afterwards, we could pass it a lambda that would create the binding. Fragment has its view, so
Fragment.getView()should return it afteronCreateView.1
u/krage Feb 10 '20
Am I missing some extra level of kotlin magic here? It doesn't look to me like that implementation of
AutoClearedValueaccepts a block for lazy evaluation like that (though you could maybe rebuild it to do so). It looks more like alateinit varplus lifecycle awareness for the autoclear so you're still responsible for assigning it inonCreateViewor wherever.1
u/Zhuinden Feb 11 '20
I figured the linked code can be easily changed to accept a block for lazy evaluation, but I'm also starting to think I was wrong to assume that'd be visible from a glance. The
by viewBindingdoesn't exist either but that doesn't make it impossible, based on the linked code (where the most interesting thing is the auto-observation using TWO lifecycles)2
u/krage Feb 11 '20 edited Feb 11 '20
Yeah that whole lifecycle block is very interesting and dense. I'm actually curious as to why the outermost observer is necessary... is it just that
fragment.lifecycleis available at that stage of the fragment's init butfragment.viewLifecycleOwnerLiveDataisn't? Might have to play around with this tomorrow.Edit: Looks like
viewLifecycleOwnerLiveDatais assigned in fragment's init so observing it in the delegate's init wouldn't be an immediate problem. It seems to work fine without the outer lifecycle observer across some basic nav between fragments for me. Maybe the outer observer could still be needed in a case where the fragment reaches destroyed state removing the observer onviewLifecycleOwnerLiveDatabut the fragment object actually gets kept and reused? That feels like a reach and probably shouldn't happen but I don't know for sure that it can't ever happen...At any rate I made minor modifications for a lazy version of the delegate. Used with bindings it feels clean not requiring nullable/lateinit/etc. on the binding in the fragment. Needing an optional second block to do cleanup equivalent to what might previously have been done in the fragment's
onDestroyViewis a bit awkward though.1
2
u/[deleted] Feb 10 '20
What's the pro of using this vs just going with the view binding from KTX?
As far as I see it is more or less the same amount of boilerplate code and they both achieve the same thing.