google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.43k stars 2.01k forks source link

Fatal Exception: java.lang.IllegalStateException:Can not perform this action after onSaveInstanceState #3115

Closed xyz-fly closed 2 years ago

xyz-fly commented 2 years ago

androidx.savedstate.SavedStateRegistry.runOnNextRecreation (SavedStateRegistry.java:168) androidx.lifecycle.SavedStateHandleController.tryToAddRecreator (SavedStateHandleController.java:109) androidx.lifecycle.SavedStateHandleController.create (SavedStateHandleController.java:71) androidx.lifecycle.AbstractSavedStateViewModelFactory.create (AbstractSavedStateViewModelFactory.java:67) androidx.lifecycle.AbstractSavedStateViewModelFactory.create (AbstractSavedStateViewModelFactory.java:84) dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create (HiltViewModelFactory.java:109) androidx.lifecycle.ViewModelProvider.get (ViewModelProvider.kt:171) androidx.lifecycle.ViewModelProvider.get (ViewModelProvider.kt:139) androidx.lifecycle.ViewModelLazy.getValue (ViewModelLazy.kt:44) androidx.lifecycle.ViewModelLazy.getValue (ViewModelLazy.kt:31) com.leon.ui.SplashActivity.getModel (SplashActivity.kt:36)

the 36 line code is: private val model: SplashActivityModel by viewModels()

the all code is: `@AndroidEntryPoint class SplashActivity : AppCompatActivity() {

private lateinit var firebaseAnalytics: FirebaseAnalytics
private val model: SplashActivityModel by viewModels()
private val binding: ActivitySplashBinding by lazy { ActivitySplashBinding.inflate(layoutInflater) }
private val permissionHandler = PermissionHandler(this)

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(binding.root)
    model.init()
}

}`

the version is 2.40.4

Chang-Eric commented 2 years ago

Is there more to that stack trace? I would guess what is going on is that you may be trying to get the model at some point other than onCreate() which is creating the view model at a time in the lifecycle that it isn't allowed to be created in. This likely isn't a Hilt issue but just an issue with when you are trying to create the ViewModel.

bcorso commented 2 years ago

Closing this for now, but feel free to reopen if you are still having this issue and have more information you can provide.