I am new to RxJava and use this together with MVP architecture.
I found some examples of saving what was observed when changing the configuration using the saved snippet (still not sure if this is the best way to do this). An example I found is the processing of observable data directly from an Activity or Fragment, and not from a presenter.
So, I experimented and set up this quick example (using only Reactivex RxJava and RxAndroid lib) for testing, which seems to work fine. What this example does:
- Triggers an action with a headless snippet saved.
- Button
- The host calls FakeService for a deferred response (5 seconds).
- The host does .cache () on this observable.
- The host reports that this save is saved.
- View saves the observed in the saved fragment.
- Presenter subscribes to observables.
- The user performs a configuration change (device rotation). The user can do this as many times as he wants.
- OnPause tells Presenter CompositeSubscription to clear and unsubscribe from all current subscriptions.
- Activity is restored and reuses the existing surviving fragment.
- Activity onResume checks if the saved fragment is stored observable zero.
- If not null, it tells the subscriber to subscribe to it.
- The saved observable is signed and, since .cache was called, it simply repeats the result for the new subscriber without calling the service again.
- When the presenter displays the final result in the view, it also saves the saved fragment with the saved observable null value.
I am wondering if I am doing this correctly, and if there is a more efficient or elegant way to handle configuration changes when the observed subscription is processed in the presenter?
Edit: Thanks for the reply. Based on this, I have achieved what, in my opinion, is a cleaner solution, and I updated the related change example.
With a new change; instead of passing the Observable from Presenter to Activity to the saved Fragment, which should be saved in case of the configurationChange event, I rather set the saved fragment as the second “view” for the Presenter when it was created.
Thus, when onResume () happens after the device rotates, I don’t need to do an Activity so that the ugly plumbing transfers the Observable from the saved fragment back to the Presenter.
The host can simply interact with this second “view” directly and verify the stored observable itself and, if necessary, re-sign. The main activity should no longer be aware of this observable. Suddenly this is a much simpler layer.
android mvp rx-java rx-android
jesobremonte
source share