Skip to content

singleton viewmodel becoming unable to launch coroutine after popBackStack #2310

@mannyvw

Description

@mannyvw

Describe the bug
Happy to be told its something im doing wrong but this seems very strange to me took me a while to track down the cause but basically ViewModels getting corrupted ? on use on popBackStack or swipe left when created vm singletons using singleOf(::NameViewModel), leaving the viewmodel not able to launch coroutines. Am guessing its just because detailscreen goes out of scope with VM attached to it, but as its a singleton it should still be available elsewhere ?

To Reproduce
Steps to reproduce the behavior:

  1. Create vm using singleOf(::NameViewModel)
  2. Add two screens that navigate from one to other
  3. reference vm in 2nd screen and 1st (should be same vm)
  4. use swipe left on android to navigate back to root screen (or can use popBackStack on a button)

Expected behavior
vm is still available to launch coroutines in vm. Coroutine just don't start

`
class MyViewModel : ViewModel() {
var name = "Hello"
private var _coroutineRunCount = MutableStateFlow(0)
var coroutineRunCount : StateFlow = _coroutineRunCount

fun runCoroutine() {
    println("runCoroutine")
    viewModelScope.launch {
        println("runCoroutine - Launched *****")
        _coroutineRunCount.value += 1
    }
}

}
**Koin module and version:** [e.g]:koin-core:4.1.1`

Snippet or Sample project to help reproduce
WeirdIssueApp 3.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions