nitrog42
05/04/2022, 1:57 PMSavedStateHandle.saveable
API, is there an equivalent to
val argArticleId = savedStateHandle.getStateFlow(ArticleArgKey, -1)
that return a State ?
I tried with .saveable(ArticleArgKey)
but it seems the return value is a Bundle instead of a Long... (in my case)Ian Lake
05/04/2022, 2:47 PMnitrog42
05/04/2022, 2:47 PMnitrog42
05/04/2022, 2:48 PMIan Lake
05/04/2022, 2:49 PMIan Lake
05/04/2022, 2:49 PMnitrog42
05/04/2022, 2:57 PM@HiltViewModel
class MyViewModel @Inject constructor(
private val savedStateHandle: SavedStateHandle
) : ViewModel() {
val someItemId: StateFlow<Long> = savedStateHandle.getStateFlow(ItemIdArg, -1L)
/**
* Some State
*/
var someState: SomeState by mutableStateOf(SomeState())
private set
/**
* Some state for the UI
*/
var someSavedState: SomeState by savedStateHandle.saveable(key = "SomeState", stateSaver = SomeState.Saver, init = {
mutableStateOf(
SomeState()
)
})
private set
}
We can expose the state either through mutableStateOf
directly, or use the new saveable
api to get it saved in the `SavedStateHandle`/`rememberSaveable`
now my Screen receive an arg through Navigation, let’s say a `detailId`;
You can get this arg using simply handle.getStateFlow(ArgKey)
and I was wondering if there was a similar helper that returns a Compose State<>
/ MutableState<>
(so we can use it directly from a Composable
, or use it in the ViewModel
, the same as the SomeState
I have)nitrog42
05/04/2022, 2:58 PMNavArgs
through SavedStateHandle
with `LiveData`/`StateFlow`, and I was wondering if there was a similar api for `State`/`MutableState`Ian Lake
05/04/2022, 3:16 PMnitrog42
05/04/2022, 3:18 PM