I would strongly advice against trying to implement this approach.
You're effectively trying to use DI framework in order to support Activity
specific life-cycle flow, although DI frameworks are not intended to be used like this.
I recently answered another similar question in which OP tried to share state in View-Model between different Activities
. Although use cases are not identical, the general pattern is the same - attempt to delegate flow control responsibilities to DI framework, which is not a good idea.
The best approach in your case (IMHO) would be to store the current state before rotation, re-instantiate the presenter upon rotation, and then restore its state.
How you store the state during rotation depends on what exactly you're trying to preserve:
- If you need to preserve UI related state (selections, texts, position of elements, etc.) then you can use the usual
onSaveInstanceState()
and onRestoreInstanceState()
callbacks
- If you need to preserve some business related state (ongoing network requests, data, data modifications, etc.) then encapsulate this logic in a business class (e.g.
SomeBusinessUseCaseManager
) and inject this class from Application
wide component with a scope.
You can find a detailed review of Dagger's scopes here.
More information about DI in Android can be found here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…