0

I have a MutableStateFlow value coming from PreferencesDataStore.

My Default value is "Newspaper.Undefined" and i can expose a new value when i'm clicking a button (from Newspaper.Undefined to Newspaper.SecondValue).

The probleme is, when i'm navigating to another screen with BottomNavigationBar and i coming back again to my Newspaper Screen, the value is flicking from Undefined to SecondValue each time. How can i prevent this, and make my flow retain my value all the time.

Thank you !

Here is my NewsPaper Screen ->

@Composable
fun Bookmark(
    viewModel: BookmarkViewModel = getViewModel(),
    paddingValues: PaddingValues,
    onArticleDetails: (String) -> Unit,
    onLogin: () -> Unit,
    onLocality: () -> Unit,
    onQuit: () -> Unit
) {
    val newsPaperFlow = viewModel.newspaperFlow.collectAsStateWithLifecycle(initialValue = Newspaper.Undefined)

    HeadBar(
        newspaperFlow = newsPaperFlow,
        onLocality = {
            onLocality()
        }
    )
}

Here is my viewModel ->

class BookmarkViewModel @Inject constructor(
    private val preferencesDataStoreRepository: PreferencesDataStoreRepository
) : ViewModel() {
    val newspaper: Flow<Newspaper> = preferencesDataStoreRepository.newsPaperFlow
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Mehdi.ncb
  • 346
  • 3
  • 13

0 Answers0