0
        @Composable
        fun SplashScreen(
            viewModel: SplashViewModel = hiltViewModel(),
            navController: NavController,
           
        ) {
        Log.e("SplashScreen", "In SplashScreen")
            val state by viewModel.getDBrow().collectAsStateWithLifecycle() //collect database empty or not
            render(state = state, navController = navController) //when success go homescreen
        }
    
    @HiltViewModel
    class SplashViewModel @Inject constructor(private val repository: Repository) : ViewModel() {
    
        override fun onCleared() {
            Log.e("SplashViewModel", "super.onCleared()")
            super.onCleared()
        }
        fun getDBrow(): StateFlow<DataState> =
            repository.getRowCount().map {
                if (it < 2) { DataState.Loading as DataState
                } else {
                    DataState.Success(
          
                        emptyList(),
                        "yay it success",
                    ) as DataState
                } as DataState
            }.stateIn(viewModelScope, initialValue = DataState.Loading, started = SharingStarted.Lazily)
    }

@Composable
fun HomeScreen(navController: NavController, viewModel: HomeViewModel = hiltViewModel()) {
    Log.e("HomeScreen", "In Home screen")
    Text(text = "Hello Hello")

}

TLDR: I did 'collectAsStateWithLifecycle()' in composable like in How to safely (lifecycle aware) .collectAsState() a StateFlow? but it is not working. This SplashScreen recompose itself after app navigate to HomeScreen.( App start SplashScreen, compose, navigate HomeScreen and restart SplashScreen compose and navigate HomeScreen in a cycle which I don't want). getDBrow is simply in dao (then repository) like:

 @Query("SELECT COUNT(*) FROM table")
    fun getCount(): Flow<Int>

I need to get Flow data from Room. Why do 'val state by viewModel.getDBrow().collectAsStateWithLifecycle()' not work as lifecycle aware? Why do it not shutdown? Am I missing something?

Non-stop log is like:

E/Navigation: to Home
E/HomeScreen: In Home screen
E/Navigation: to Home
E/HomeScreen: In Home screen
E/Navigation: to Home
E/HomeScreen: In Home screen
E/Navigation: to Home
E/HomeScreen: In Home screen
E/Navigation: to Home
E/HomeScreen: In Home screen
E/SplashScreen: In SplashScreen

Edit: Added an Sample Project to Github.

Non working Sample Project

yardımcı Etis
  • 140
  • 2
  • 12
  • 1
    check out [this answer](https://stackoverflow.com/a/69491725/3585796) – Phil Dukhov Apr 13 '23 at 03:49
  • is DataState.Success -> { Log.e("SplashScreen", "to HOME") LaunchedEffect(viewModel.dataState) { Log.e("SplashScreen", "to HOME in LaunchedEffect ") navController.navigate("home") } } I did like that. it seems it stops bleeding but it recompose again before going Homescreen. I tried to escape it and try state control, but i couldn't – yardımcı Etis Apr 13 '23 at 15:39
  • Logs are like : to HOME, to HOME, in LaunchedEffect to HOME, In Home screen, In Home screen, – yardımcı Etis Apr 13 '23 at 15:42
  • 1
    my answer explains why it recomposes multiple times. if it helped you please give it an upvote. – Phil Dukhov Apr 14 '23 at 00:47

1 Answers1

1

Your getDBrow() function creates a brand new StateFlow instance each time it's called, so you always have new state, and you also have a memory leak that grows on each recomposition. Change it to a property that is initialized only once. Here I also specified the map return type so you don't need the casts.

val dBrow: StateFlow<DataState> =
    repository.getRowCount().map<_, DataState> {
        if (it < 2) { DataState.Loading
        } else {
            DataState.Success(
                emptyList(),
                "yay it success",
            )
        }
    }.stateIn(viewModelScope, initialValue = DataState.Loading, started = SharingStarted.Lazily)
Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • Thank you for map cast tip but I get same result with dBrow. you may try. I upload to github. https://github.com/LearningStuffetc/FlowWork. – yardımcı Etis Apr 13 '23 at 01:20