Hi, everyone! I need to create a UI wiget that wil...
# android
v
Hi, everyone! I need to create a UI wiget that will hold an observable reference to a small portion of my state and display it with a custom layout. This wiget may be used inside an Activity or a ResycleView. In modern Android, can I do it? I've tried a view, but it doesn't support lifecycle on its own. I've tried a fragment, but it seems to be unusable from a ResycleView
p
Is totally doable. How/When is the observable source injected in your CustomView? You can extend from FrameLayout and overide `onAttach`/`onDetach` and
onLayout
methods.
v
@Pablichjenkov that would manually recreate lifecycle for the view, correct?
p
I don't quite get what do you mean by:
manually recreate lifecycle for the view
You can create a CustomViewGroup extending FrameLayout for simplicity. Then at some point in your Activity or RecyclerView.ViewHolder code you will inject this Data Observable you refer too. I would wait until at least one layout pass on my CustomFrameLayout. This is just to guarantee that I don't make View updates before the first Layout. For detecting this you can set a flag in the
onLayout
overide. After the CustomFrameLayout is Layout then I would subscribe to the Observable to start receiving events. Every time a new event arrives then update your View. Depending on the type of subviews you are updating you may need to call `invalidate()`inside your CustomFrameLayout. OnAttach/onDetach are called when your View is remove from the ViewTree. In most cases due to ViewTree destruction. You can also send an
onComplete()
signal from the data source to the View to indicate completion. If you want a more fine control over the specific Activity LifeCycle then you will need a mechanism to propagate them up to the View manually. There is no a nice class that propagate them to the View. I think it was one of the initial handy features about Fragments.
c
personally, I'd approach this by having explicit methods on the custom view to bind and unbind its data source, if the view should be handling updating itself at all. If you have it in a recyclerview you'll be wanting to bind and unbind when the adapter asks you to. If the view was in a fragment, then you use onResume and onPause as an indicator though using the lifecycle management library would probably be the best bet.
v
There is no a nice class that propagate them to the View. I think it was one of the initial handy features about Fragments.
That's what i have been searching for. Good to know that there isn't one from Google. Will try to implement myself or find a 3rd party one
using the lifecycle management library would probably be the best bet.
I am trying to use lifecycle components the best i can. How would you implement it? Pass it though from the activity or implement a new lifecycle object?
s
Can you give the View a view model type thing, and have the view model observe the lifecycle?
v
@steve AFAIU referencing a lifecycle object in viewmodel is an anti pattern
Caution: A ViewModel must never reference a view, Lifecycle, or any class that may hold a reference to the activity context.