voyager icon indicating copy to clipboard operation
voyager copied to clipboard

LocalViewModelStoreOwner inside AndroidScreen should implement HasDefaultViewModelProviderFactory and return SavedStateViewModelFactory

Open equeim opened this issue 3 years ago • 0 comments

When androidx.lifecycle.viewmodel.compose.viewModel() function is called inside plain ComponentActivity or Fragment, it uses ViewModelStoreOwner of activity/fragment which implements HasDefaultViewModelProviderFactory and returns SavedStateViewModelFactory. This allows using ViewModel that accepts SavedStateHandle or Application in constructor without any configuration.

If one then decides to migrate to Voyager library and use AndroidScreen, they would encounter runtime exceptions because AndroidScreen's ViewModelStoreOwner implementation doesn't implement HasDefaultViewModelProviderFactory which makes viewModel() use no arguments constructor.

AndroidScreen should use SavedStateViewModelFactory by default to match behaviour of ComponentActivity/Fragment (there also needs to be an API to pass Bundle of default state to SavedStateViewModelFactory).

equeim avatar May 24 '22 22:05 equeim