You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There's always need for communication, right 😉 Suppose we have OnboardingActivity that has several OnboardingFragment. Each Fragment has a startButton telling that the onboarding flow has finished, and only the last Fragment shows this button.
Here are several ways you can do that
1. EventBus 🙄
Nearly all articles I found propose this https://github.com/greenrobot/EventBus, but I personally don't like this idea because components are loosely coupled, every component and broadcast can listen to event from a singleton, which makes it very hard to reason when the project scales
We can use simple PublishSubject to create our own RxBus
importio.reactivex.Observableimportio.reactivex.subjects.PublishSubject// Use object so we have a singleton instanceobject RxBus {
privateval publisher =PublishSubject.create<Any>()
funpublish(event:Any) {
publisher.onNext(event)
}
// Listen should return an Observable and not the publisher// Using ofType we filter only events that match that class typefun <T> listen(eventType:Class<T>): Observable<T> = publisher.ofType(eventType)
}
This is advised here Communicating with Other Fragments. Basically you define an interface OnboardingFragmentDelegate that whoever conforms to that, can be informed by the Fragment of events. This is similar to Delegate pattern in iOS 😉
We can learn from Share data between fragments to to communication between Fragment and Activity, by using a shared ViewModel that is scoped to the activity. This is a bit overkill
classOnboardingSharedViewModel: ViewModel() {
val finish =MutableLiveData<Unit>()
}
I don't think there's a silver bullet here but the actual implementation highly aligns to your app's overall architecture.
I personally would try to avoid implicitly dispatching events like done 1 to 3 as there's a risk someone might subscribe to those events who's actually not concerned of consuming them.
If you had an MVP architecture there would probably be a presenter coordinating this. With an MVVM architecture maybe you've got an observable you can observe. That said, I think most of these solutions are find, I would just avoid any that expose those events outside of it's own concern.
There's always need for communication, right 😉 Suppose we have
OnboardingActivity
that has severalOnboardingFragment
. EachFragment
has astartButton
telling that the onboarding flow has finished, and only the lastFragment
shows this button.Here are several ways you can do that
1. EventBus 🙄
Nearly all articles I found propose this https://github.com/greenrobot/EventBus, but I personally don't like this idea because components are loosely coupled, every component and broadcast can listen to event from a singleton, which makes it very hard to reason when the project scales
Read more
2. Otto 🙄
This https://github.com/square/otto was deprecated in favor of RxJava and RxAndroid
3. RxJava 🙄
We can use simple
PublishSubject
to create our own RxBus4. Interface
This is advised here Communicating with Other Fragments. Basically you define an interface
OnboardingFragmentDelegate
that whoever conforms to that, can be informed by theFragment
of events. This is similar toDelegate
pattern in iOS 😉5. ViewModel
We can learn from Share data between fragments to to communication between Fragment and Activity, by using a shared
ViewModel
that is scoped to the activity. This is a bit overkillNote that we need to call
ViewModelProviders.of(activity)
to get the sameViewModel
with theactivity
7. Lambda
Create a lambda in
Fragment
, then set it ononAttachFragment
. It does not work for now as there is noOnboardingFragment
inonAttachFragment
😢8. Listener in Bundle 🙄
Read more
The text was updated successfully, but these errors were encountered: