TextFieldState.Saver


Saves and restores a TextFieldState for rememberSaveable.

A restored state has no onTextChanged callback. To restore a state that was created with onTextChanged, use the Saver returned by TextFieldState.Saver(onTextChanged).

Summary

Public functions

operator Saver<TextFieldState, Any>
invoke(onTextChanged: ((CharSequence) -> Unit)?)

Returns a Saver that saves a TextFieldState the same way as Saver and passes onTextChanged to the restored state.

Cmn
open TextFieldState?
restore(value: Any)

Convert the restored value back to the original Class.

Cmn
open Any?

Convert the value into a saveable one.

Cmn

Public functions

invoke

operator fun invoke(onTextChanged: ((CharSequence) -> Unit)?): Saver<TextFieldState, Any>

Returns a Saver that saves a TextFieldState the same way as Saver and passes onTextChanged to the restored state.

A lambda cannot be saved, so a state restored by Saver has no onTextChanged. Use this function to save a state that was created with onTextChanged, for example with rememberSaveable(saver = TextFieldState.Saver(onTextChanged)). rememberTextFieldState does this.

onTextChanged is called after each commit that changes the text of the restored state: user input, TextFieldState.edit and its helpers, and undo/redo. It is not called when only the selection, composition, or styles change. onTextChanged runs on the thread that made the edit, before the edit call returns. When onTextChanged runs, TextFieldState.text and TextFieldState.selection show the new value, the undo entry is recorded, and the IME has been notified.

onTextChanged must not change the state. Calling TextFieldState.edit, its helpers, or UndoState.undo and UndoState.redo on the state from onTextChanged throws an IllegalStateException. To intercept and modify user input, use InputTransformation.

The CharSequence passed to onTextChanged is provided for convenience and has the same text content as TextFieldState.text.

Parameters
onTextChanged: ((CharSequence) -> Unit)?

Called after each commit that changes the text of the restored state, receiving the new text with the same content as TextFieldState.text.

restore

open fun restore(value: Any): TextFieldState?

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.

SaverScope.save

open fun SaverScope.save(value: TextFieldState): Any?

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