Consider the following situation. I have an application scoped repository, dependent on a Retrofit client for supplying some of its resources. Retrofit on the other hand, because the API requires authentication, is dependent on an authentication key obtained from the shared preferences. The authentication key can be invalidated at any time. If the repository tries to make an unauthenticated request, the login activity is invoked. The login activity refreshes the authentication key and switches back to the home activity of the application. The problem which arises at this stage, is that only after the application is killed and restarted the Repository will be recreated, and thus the authentication key of the Retrofit client refreshed.
One inelegant solution would be, to change the scope of the repository from the application level to the activity level. However this would create unnecessary copies of the repository. Is there a way to refresh parts of the dependency tree? (Think something like an invalidate method on a singleton, setting its instance to null).
What is the general correct approach if only one object of a type is necessary in an application, but one of the dependencies of this object is fundamentally mutable? Should possibly mutable elements ever be injected?