iex
04/16/2020, 5:42 PMiex
04/16/2020, 5:50 PMonCleared to call the delegate's clearDisposables .Cody Engel
04/16/2020, 5:53 PMiex
04/16/2020, 5:53 PMiex
04/16/2020, 5:54 PMonCleared) though that's hackyCody Engel
04/16/2020, 5:56 PMDisposableHandler, and then that would maintain creating the CompositeDisposable and ensuring the correct function is invoked.
I used this at a previous company to share code with the Activity, Fragment, and ViewModel. Albeit there is a bit of ceremony going on and you'd still likely end up with a DisposableActivity, DisposableFragment, and DisposableViewModel, but that's one way delegation could play into it.Cody Engel
04/16/2020, 5:57 PMonCleared function also works though, especially if you just want to share it between ViewModels. Happy to hear the articles helped you out, seeing that made my day 🙂iex
04/16/2020, 6:03 PMonCleared function via interface composition or delegates though. The compiler wouldn't detect if the view model's onCleared was renamed, for exampleiex
04/16/2020, 6:05 PMonClear tooCody Engel
04/16/2020, 6:10 PMiex
04/16/2020, 6:16 PMonClear() (or equivalent) to call the interface's method, so it's pretty much the same amount of code than without the interface.Cody Engel
04/16/2020, 6:33 PMinterface DisposableHandler {
fun addDisposable(disposable: Disposable)
fun addDisposables(disposables: List<Disposable>)
fun clearDisposables()
}
Then the implementation (what would be delegated to the Activity, Fragment, ViewModel) would use a CompositeDisposable as the property to proxy those. There are some more advanced techniques you could use, such as having Application.ActivityLifecycleCallbacks registered to invoke clearDisposables when onDestroyed is called. There is a similar callback for Fragments too, but I'm unsure if ViewModels have anything similar... That'd be one way to avoid having a base class though.iex
04/16/2020, 6:37 PMclearDisposables on the interface, which would result in the same amount of code than just managing them itself (assuming usage of CompositeDisposable)
ActivityLifecycleCallbacks would probably improve this though.Cody Engel
04/16/2020, 6:40 PM