[Android] ViewModel-SavedState-ktx 1.0.0-beta01 Released

Takumi WADA
ViewModel-SavedState-ktx
2 min readOct 12, 2019
ViewModel-SavedState-ktx 1.0.0-beta01

# What is ViewModel-SavedState-ktx

A library that hides the SavedStateHandle of ViewModel-SavedState so that it can be easily handled by Delegated Property.

# About ViewModel-SavedState-ktx

# About ViewModel-SavedState-ktx 1.0.0-beta01

## Library update

- `androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-beta01`

## enum support

An enum can be passed to an Activity Intent or Fragment Bundle and referenced in the ViewModel. You can pass enum to Intent or Bundle with extension function as follows.

// Intent
intent.putExtraEnum("key", YourEnum.ENUM_VALUE)
// Bundle
bundle.putEnum("key", YourEnum.ENUM_VALUE)

In ViewModel, you can access to enum as follows.

class YourViewModel(
savedStateHandle: SavedStateHandle
) : SavedStateViewModel(savedStateHandle) {
// Property
var yourEnum: YourEnum by savedStateProperty("key")
// LiveData
val yourEnumLiveData: MutableLiveData<YourEnum> by savedStateLiveData("key")
}

## Support custom delegate

You can set serialization / deserialization and save / restore any type.

class YourViewModel(
savedStateHandle: SavedStateHandle
) : SavedStateViewModel(savedStateHandle) {
// Property
var yourClass: YourClass by savedStateProperty<String, YourClass>({
Json.parse(YourClass.serializer(), it)
}, {
Json.stringify(YourClass.serializer(), it)
}, "key")
// LiveData
val yourClass: MutableLiveData<YourClass> by savedStateLiveData<String, YourClass>({
Json.parse(YourClass.serializer(), it)
}, {
Json.stringify(YourClass.serializer(), it)
}, "key")
}

# Click here for ViewModel-SavedState-ktx 1.0.0-beta01

--

--