ScreenModel
The ScreenModel API is a part of the modulecafe.adriel.voyager:voyager-screenmodel
(see Setup).
ScreenModel
is just like a ViewModel: designed to store and manage UI-related data in a lifecycle conscious way. It also allows data to survive configuration changes such as screen rotations.
Unlike ViewModel
, ScreenModel
is just an interface. It's also Android independent and doesn't requires an Activity
or Fragment
to work.
By design, it's only possible to create a ScreenModel
instance inside a Screen
. Call rememberScreenModel
and provide a factory lambda.
If you need to have multiple instances of the same ScreenModel
for the same Screen
, add a tag to differentiate them.
Sample
Source code here.
Navigator scoped ScreenModel
TherememberNavigatorScreenModel
are part of the navigator library.
Starting from 1.0.0rc08
by using the new Navigator extension called rememberNavigatorScreenModel
is possible to have a ScreenModel that is shared cross all Screens from a Navigator and when the Navigator leaves the Composition the ScreenModel is disposed.
Last updated