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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>