Reputation: 320
I'm working on custom view that will be used as normal android view component. Is it possible to internally handle hosts (activity/fragment) lifecycle states?
My goal is to avoid end users (devs) to override every lifecycle callback in order to sync states with my view, for example:
@Override
protected void onStop() {
super.onStop();
if (myCustomView != null) {
myCustomView.onStop();
}
}
Upvotes: 0
Views: 2815
Reputation: 61
If you are using Architecture Components you can implement LifecycleObserver interface, then:
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
((LifecycleOwner)context).getLifecycle().addObserver(this)
Upvotes: 1