A lot of people are wondering why use a bus in my Clean Android implementation (Clean-Contacts repo on GitHub). And the reply is simple: I need to to get the reply of a previously launched interactor in a new instance of a presenter because on a rotation change the presenter is destroyed also the view, etc…
That was yesterday, because today we (Sergio and me) found a solution (I’m testing it at this moment). I read the LoaderManager code in the AOSP and I found a possible solution. The solution is basically to use a Handler to manage the orientation changes on an Activity. My BaseActivity has an instance of “ConfigurationKeeper” wich is a class that extends Handler. The onDestroy method of the Activity calls to the handler with a message to destroy the Dagger’s activityGraph BUT the onCreate removes all callbacks of the Handler. So if the device is rotating the handler will not delete the dagger module but if the Activity is destroying the handler will execute the method for destroy Activity Graph module and later will set to null this handler to avoid leaks. The graph is retained using “onRetainCustomNonConfigurationInstance()” of the Activity (and no, is not deprecated).
This is a test implementation and I’m looking for feedback.
You can read this code in the GitHub branch “develop” and give some feedback at the comments or in Github 🙂
Feedback from Michal Charmas:
Michal gave me some tips on issue #3 and he is right. Resuming: Since we now use “onRetainCustomNonConfigurationInstance” method from Android to retain the whole graph, we don’t need ConfigurationKeeper nevermore. If you are holding a reference on the graph to the Activity (MainView for ex) since we are keeping the graph on configuration changes, the reference is leaking, so 2 options, don’t hold references to the activity or destroy the references in the “onDestroy” method. We’re retaining the graph and the presenter, the presenter needs a view to work, but later than onDestroy we need to setup the new view to the already created presenter just calling “onCreate(presenterView)” new method. Also, we call “onDestroy” on the presenter to set the view to null.