CXwudi
06/28/2024, 2:22 AMCXwudi
06/28/2024, 2:23 AMfun initKoin(ctx: Context) = koinApplication {
androidContext(ctx)
modules(allModules)
}
class TestMainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val koin = initKoin(this@TestMainActivity).koin
// some Decompose library stuff are involved, but unrelated to this question
val rootComponent = DefaultRootComponent(defaultComponentContext(), koin)
setContent {
MyUI(rootComponent)
}
}
}
And in my Koin container, I have a dependency that simply do a counter++
every 1 second, like this:
class SampleCounterServive {
// again, some Decompose stuff involved but unrelated to this question
private val _counter = MutableValue(0)
val counter: Value<Int>
get() = _counter
init {
CoroutineScope(Dispatchers.Default).launch {
flow {
while (true) {
delay(1000)
emit(Unit)
}
}.collect {
_counter.value++
}
}
}
}
Now, when I trigger a configuration change (rotate the phone), based on my understanding, the onDestroy()
is called and then onCreate()
is called again to recreate the activity. Then the initKoin
is called again so SampleCounterServive
should be reset to 0. But what I observed is that the counter continues from where it was even after the configuration change.
So my question, does Koin automatically handle configuration changes for the dependencies in the container? if so then how does it work in detail? And if not, what is causing my dependency to survive the configuration change?Pablichjenkov
06/28/2024, 4:20 AMCXwudi
06/28/2024, 4:32 PM<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="<http://schemas.android.com/apk/res/android>">
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:enableOnBackInvokedCallback="true">
<activity
android:name="TestMainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Really don't think there is anything that modifies the default configuration changes behaviour
Beside this, you answer hinted me there is potential something else (maybe Decompose) that made the surviving, I will need a minimal reproducible sample to continue
Thanks for your hintPablichjenkov
06/28/2024, 5:38 PM