Android: How to retain Dagger 2 dependency graph through configuration changes
Problem:
-
User is prompted to fill up a form.
-
User presses 'Submit' button.
-
A progress bar is displayed while the network operation happens in the background.
-
User rotates the screen before the network request completes.
-
The progress bar is gone although the network operation is still happening.
-
User is not notified of the result from the network.
Solution: Retain Dagger component through configuration changes. After rotation we just attach the new Activity/Fragment/Custom View. The Observable (if you use Rx) is retained through configuration changes so it's the same instance of the Object that originated the request, therefore, by re-attaching to it we can obtain the result.
It works in a similar fashion as Loaders to retain state through configuration changes.
UPDATE: I would advise to use ViewModel from Google Architecture Components instead.