google / dagger

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

State inside FeatureModule of Hilt simple example is not persisted across process death #1866

Open Zhuinden opened 4 years ago

Zhuinden commented 4 years ago

https://github.com/google/dagger/blob/c1ddd0cfeddaa286e6826d9527a108261f6d4fc3/java/dagger/hilt/android/example/gradle/simple/feature/src/main/java/dagger/hilt/android/example/gradle/simple/feature/FeatureModule.kt#L30

Not persisting such state can cause unexpected behaviors and bugs, especially if this is used to share state between screens.


This raises following questions:

1.) if ViewModel is used under the hood for ActivityRetainedComponent, then how do I intercept onCleared() in my custom classes?

2.) if ViewModel is used under the hood for ActivityRetainedComponent, then how do I get a SavedStateHandle into my custom classes?

3.) if ViewModel is not used for ActivityRetainedComponent, how do I get a reference to the SavedStateRegistry into my custom classes?


Overall, the question boils down to:

Chang-Eric commented 4 years ago

Just FYI, we're going to come back to this issue about Hilt after the official launch. Thanks for filing it though and sorry for the wait!

Zhuinden commented 4 years ago

It's fine, thanks for the reply and will look forward to it 🙂

Chang-Eric commented 4 years ago

Coming back to this, we have plans to make a SavedStateHandle binding available in the ActivityRetainedComponent. You're right though that there is currently no way to handle process death in the ActivityRetainedComponent right now unfortunately.

Zhuinden commented 4 years ago

we have plans to make a SavedStateHandle binding available in the ActivityRetainedComponent.

That would solve it!

BTW, is ActivityRetainedComponent deps inherited into @ViewModelInject annotated ViewModels?

Chang-Eric commented 4 years ago

Yes, @ViewModelInject classes inject themselves from the ActivityRetainedComponent. This is true regardless of what ViewModelOwner is used.