Google is committed to advancing racial equity for Black communities. See how.

Saver

interface Saver<Original, Saveable : Any>
androidx.ui.savedinstancestate.Saver

The Saver describes how the object of Original class can be simplified and converted into something which is Saveable.

What types can be saved is defined by UiSavedStateRegistry, by default everything which can be stored in the Bundle class can be saved. The implementations can check that the provided value can be saved via SaverScope.canBeSaved

You can pass the implementations of this class as a parameter for savedInstanceState or rememberSavedInstanceState.

import androidx.ui.savedinstancestate.Saver

data class Holder(var value: Int)

// this Saver implementation converts Holder object which we don't know how to save
// to Int which we can save
val HolderSaver = Saver<Holder, Int>(
    save = { it.value },
    restore = { Holder(it) }
)

Summary

Public methods
abstract Original?
restore(value: Saveable)

Convert the restored value back to the original Class.

abstract Saveable?
SaverScope.save(value: Original)

Convert the value into a saveable one.

Public methods

restore

abstract fun restore(value: Saveable): Original?

Convert the restored value back to the original Class. If null is returned the value will not be restored and would be initialized again instead.

save

abstract fun SaverScope.save(value: Original): Saveable?

Convert the value into a saveable one. If null is returned the value will not be saved.