My activity code:
class MyActivity : AppCompatActivity(){
private val viewModel : SharedViewModel by viewModels()
//***
//***
//***
}
My fragment code:
class MyFragment : Fragment(){
private val viewModel : SharedViewModel by activityViewModels()
//***
//***
//***
}
When I create the ViewModel within the activity only then everything works fine. But when I try to create it also in the activity's fragment then I have the error:
Caused by: android.view.InflateException: (...) Error inflating class fragment
SharedFragment.getViewModel(SharedFragment.kt:23)
SharedFragment.onViewCreated(SharedFragment.kt:23)
Caused by: java.lang.IllegalArgumentException: CreationExtras must have a value by `SAVED_STATE_REGISTRY_OWNER_KEY`
MyActivity.onCreate(MyActivity.kt:60)