0

This might be a wrong approach, but I am trying to find a way to re-instantiate an underlying Retrofit \ OkHttpClient which is annotated with @Singleton.

The use case is that on the very first app start it will have a different URL. Then after the login and consecutive launches it will use the URL from the SharedPreferences. It looks like this

@Module
public class NetModule {

    @Provides
    @Singleton
    WrapperClass providesAPI() {
        // underlying call with new Retrofit.Builder().client(okHttpClient).baseUrl(url)
        return new WrapperClass("someURLToProvision");
    }
}

And in the Activity I inject it with

 @Inject protected WrapperClass api;

After the provisioning I will no longer have the URL someURLToProvision but the actual URL which is stored in the SharedPreferences.

The question is: Can I re-instantiate the Singleton so it will not take the hard coded URL, but the one from the SharedPreference?

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • 1
    As far as I know, the only way to do that is to recreate your Dagger component as the singleton is linked to it. See [here](https://www.techyourchance.com/dagger-2-scopes-demystified/) for more info about Dagger scopes – Eselfar Nov 07 '17 at 15:00
  • your OKHTTP must be singleton but the retrofit instance must be a Non-Singleton instance. Take a look at this project it implement what you want : https://github.com/Fakher-Hakim/Firebase-Auth-Dagger-2 – Fakher Nov 07 '17 at 15:16
  • I just wanna link 2 similar questions [here](https://stackoverflow.com/q/36498131/1837367) and [here](https://stackoverflow.com/q/36769923/1837367) about how to scope/provide retrofit with different urls – David Medenjak Nov 07 '17 at 20:08

0 Answers0