Closed M-Miyazako closed 7 months ago
I had overlooked it!
When you set FragmentFactory, you need to do it before super.onCreate.
class MainActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
supportFragmentManager.fragmentFactory =
MainActivityComponent::class.create(ApplicationComponent.getInstance(this))
.fragmentFactory
super.onCreate(savedInstanceState)
}
}
I had reversed the order, which caused the problem. After I fixed it, the app no longer crashes.
I hope this helps anyone else with the same problem.
Also note that you linked to the deprecated platform Fragment, If you look at the [androidx documentation](https://developer.android.com/reference/androidx/fragment/app/Fragment#Fragment()) it does point you to FragmentFactory.
And sorry I'm going to go ahead and close this, it may be useful but it's not really related to kotlin-inject directly.
The Android documentation describes how to inject the ViewModel. https://github.com/evant/kotlin-inject/blob/main/docs/android.md#viewmodels
But, as stated in the official reference, Fragment must have an empty constructor or an exception may be raised.
https://developer.android.com/reference/android/app/Fragment.html
And the problem did indeed occur. The app crashes when switching the device to dark mode or using the split screen feature. Is there a way around this problem?