Tim Malseed
03/22/2023, 10:37 PMstateIn(viewModelScope, whileSubscribed(5000), ViewState.Loading)
Pablichjenkov
03/22/2023, 10:54 PMTim Malseed
03/22/2023, 10:55 PMPablichjenkov
03/22/2023, 11:00 PMTim Malseed
03/22/2023, 11:17 PM10:11:56.288 onDestinationChanged: home_route // start destination
10:11:56.450 AuthorizationState: Unauthorized // User is unauthorized
10:11:56.524 ViewState: Loading // Initial view state
10:11:56.803 ViewState: Login // Because user is unauthorized
10:11:56.803 navController.navigate(login_route) // Navigate to login screen
10:11:56.806 onDestinationChanged: login_route // User arrives. Presses 'login'
10:12:05.158 AuthorizationState: Authorized // Auth state changes to 'authorized'
10:12:05.726 navController.popBackStack() // go back to home screen
10:12:05.727 onDestinationChanged: home_route // we're back
10:12:05.745 ViewState: Login // ViewState is Login? Should be 'Ready'
10:12:05.745 navController.navigate(login_route) // Navigate back to the login screen
10:12:05.750 onDestinationChanged: login_route // Go to login screen
10:12:05.786 navController.popBackStack() // Oh, user is authorized, pop back again
10:12:05.788 onDestinationChanged: home_route // Back to home
10:12:05.837 ViewState: Ready // Now the home screen has the up-to-date view state
popBackStack()
Pablichjenkov
03/22/2023, 11:30 PMTim Malseed
03/22/2023, 11:32 PMPablichjenkov
03/22/2023, 11:36 PMTim Malseed
03/22/2023, 11:42 PMPablichjenkov
03/22/2023, 11:47 PMTim Malseed
03/22/2023, 11:55 PMcollectAsStateWithLifecycle()
, which resets its state when the lifecycle changesAlex Vanyo
03/23/2023, 12:54 AMWhileSubscribed(5000)
, the previous value of the StateFlow
will be cached and it won’t expire due to the default value for replayExpiration
, the second parameter of WhileSubscribed
So even if Loading
is the default state, it’ll be caching the last one seen upon returning.
Maybe for authentication state in particular, it’d be better to not cache that last value? Maybe set replayExpiration
to 0
, and maybe also stopTimeoutMillis
to 0
too specifically for the authentication:
WhileSubscribed(0, 0)
That would ensure that you’re refreshing the flow upon going back to the home screen, which will cause the initial Loading
state againTim Malseed
03/23/2023, 12:58 AMstopTimeoutMillis
to 0, but I didn’t even consider this replayExpiration
parameter! That seems to solve the problemWhileSubscribed
docs and think about this a bit moreColton Idle
03/26/2023, 2:26 PMTim Malseed
03/28/2023, 3:14 AM