Skip to content

sharedKoinViewModel fails with type-safe navigation routes #2292

@nazaburo

Description

@nazaburo

Describe the bug
The sharedKoinViewModel function in koin-compose-viewmodel-navigation doesn't work correctly with type-safe navigation (serializable routes). When the default parameter is used, it causes a runtime crash because the serializable route object from destination.parent?.route is incorrectly treated as a String route by NavController.getBackStackEntry(), leading to a type mismatch.

Environment

  • Koin version: 4.1.1
  • Navigation Compose version: 2.9.5
  • Kotlin version: 2.2.20
  • Compose BOM: 2025.10.00

The Problem
When using type-safe navigation with @serializable routes and the default parameter navGraphRoute: Any? = this.destination.parent?.route, the function receives a serializable route object but NavController.getBackStackEntry() internally treats it as if it were a String route, causing a type mismatch.

The navigation graph was created with serializable route objects, but getBackStackEntry() tries to match it as a String representation, so the entry cannot be found and crashes.

Sample Project
I've created a minimal reproduction project that demonstrates this issue:
https://github.com/nazaburo/SharedKoinViewModelSample

Proposed Fix
I've submitted a pull request with a suggested fix:
nazaburo/SharedKoinViewModelSample#2

The fix properly handles both String-based and serializable routes by detecting the route type and calling the appropriate getBackStackEntry() overload.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions