I have Hilt DI framework in my Android project. Also I have retrofit, and ApiNetworkModule for getting singleton retrofit object:
@Module
@InstallIn(SingletonComponent.class)
public class ApiNetworkModule {
...
@Singleton
@Provides
public Retrofit provideRetrofit(
OkHttpClient okHttpClient,
Gson gson,
SharedPrefManager sharedPrefManager
) {
return new Retrofit.Builder()
.client(okHttpClient)
.baseUrl(sharedPrefManager.getUrl())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
...
}
In general it works fine, but I have noticed in the case when url is updated in sharedPrefManager
, retrofit object does not know about it and uses old url. It will be fully updated only after closing-opening application. Is there any way how to reinit Retrofit
singleton programmatically? Or how to handle it correctly?