Java – the host who insists on using a dagger in the activity
When you inject a presenter into an active range rotation, how do you keep the presenter active?
My understanding is that presenters and activities are destroyed and a new presenter and activity are created each time Is there any way to keep the presenter alive while the activity is recreated?
Solution
Yes, as long as we talk about configuration changes (such as rotation)
You can override the methods of the fragmentactivity
Interesting onretaincustomnonconfigurationinstance(): any - saves the presenter's rotation
To restore the same instance during oncreate(), just call getlastcustomnonconfigurationinstance(): any
Here is an example of saving dagger 2 subcomponents in baseactivity When you use methods to save Dagger's components, not just the presenter, the creation of the presenter becomes very simple:
>Tag the presenter constructor with @ inject > tag the presenter field in the target activity with @ inject > get the injector from baseactivity (as in a linked class) and inject it in the target activity during oncreate