I still would like to fix my understanding, and wo...
# koin
c
I still would like to fix my understanding, and would appreciate any hint or answers to this question. Last time I talked in this channel about a wired problem that a value is surviving the configuration change where it shouldn't be. However, the sample code I showed contains some other libraries, so it was hard to debug. This time I brought another sample code with only Koin involved, and I still see wired configuration changes surviving. So, this time I am really confuse how does the value survive the configuration changes. Sample code in the thread
First I have a very simple counter class:
Copy code
class CountingStatefulService {
  private val _counter: MutableStateFlow<Int> = MutableStateFlow(0)
  val counter: StateFlow<Int> = _counter

  fun increment() {
    _counter.value++
  }
}
This class is not even a view model, so the
_counter
value supposed to reset to 0 when configuration changes happen. Now I have a very simple `MainActivity`:
Copy code
class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
      KoinContext(koinApplication {
        modules(allModules) // just registered a singleton of CountingStatefulService, nothing else
      }.koin) {
        MainUI {RootScreen(LocalKoinApplication.current.get())
        }
      }
    }
  }
}
Where my UI is just:
Copy code
@Composable
fun MainUI(
  content: @Composable () -> Unit
) {
  MaterialTheme {
    content()
  }
}

@Composable
fun RootScreen(counter: CountingStatefulService, modifier: Modifier = Modifier) {
  val count by counter.counter.collectAsState()
  Column {
    Text("counter = $count")
    Button(onClick = { counter.increment() }) {
      Text("increment")
    }
  }
}
With the above code, I observed that
Text
never reset to
counter = 0
when I rotate the screen, So I am really confused. Does
koinApplication
in android also did anything to survive configuration changes? If not, then what is causing the
_counter
value to survive configuration changes?
Oh well, I just discovered that I have to explicitly call `koin.close()`in
onDestroy()
to release all resources, then I can see that the
_counter
will be reset to 0 as expect
c
Where are you starting koin? Most likely in your application class, which won't be destroyed on configuration changes. Since you supplied the class as a singleton you receive the same instance each time. But in this case why would you not use a factory, or not use koin at all as the current setup will make previewing these composables pretty annoying.
c
Right now I switched to initialize Koin in application class instead of the Activity class. Previously, I initiated it by calling
koinApplication {}
in the
onCreate()
method in the Activity class, and I didn't even store the koin instance to anywhere, but just passing it to
KoinContext {}
composable function. Now, I know this is not a good practice as the Koin instance will be re-initialized upon configuration change. But at the same time, I didn't expect the
_counter
from the example above to survive the configuration change.