An application uses singletons. One of them contains an instance of Retrofit
client. Another one contains constants.
I can change a server URL in app preferences (in activity), so that after the app restart I expect to reinitialize Retrofit
client with the new base URL.
After first start all singletons and classes are initialized. Then I change the URL in the activity and restart the application. Now singletons don't initialize and contain old constants. If I forcely stop the app or attach a debugger these singletons restart as expected.
object ApiClass {
private val gsonConverter: GsonConverter = GsonConverter()
var gson: Gson
private set
var retrofit: Retrofit
private set
init {
Timber.i("*** start ApiClass")
val okHttpClient = OkHttpClient().newBuilder()
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.build()
gson = GsonBuilder().setLenient().create()
val url = "..." // Get a server URL from preferences.
Timber.i("*** " + url)
retrofit = Retrofit.Builder()
.baseUrl(url)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
...
}
}
/////////////
object ApiConst {
init {
Timber.i("*** start ApiConst")
}
const val SOME_TEXT = "text"
}
How to reinitialize singletons without Force stop
the app?