I would like to fix my understanding about android...
# koin
c
I would like to fix my understanding about android and koin integration. Would anyone help me answer this question? The question is in the reply
I setup Koin in Android in an isolated Koin context like this:
Copy code
fun 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:
Copy code
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?
p
Should work the way you describe it. Can you confirm you are not skipping configuration changes in the manifest?
c
My manifest file is very simple
Copy code
<?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 hint
p
Yeah your manifest seems to be using the default behavior. Must be something else 👍