Why when I navigate from screen A to screen B, scr...
# compose
l
Why when I navigate from screen A to screen B, screen A still recomposes? For example
Copy code
composable(A){
   // other stuff
   ScreenA( navHostController, ..., onNavigateToB = { navHostController.navigate(B) } ) 
   // after onNavigateToB executes, it recomposes and re-renders ScreenA 
}
composable(B){
   ScreenB( ... )
}
i
During the crossfade animation between screens, both screens are visible at the same time, so you should expect both to be recomposing, yes
l
Great, thanks for the quick reply! Now, since this is the expected behavior • If onNavigateToB clears something that ScreenA needed (for example, ScreenA needed a non-null string, and now that lambda removes it). Its okay/possible to skip that recomposition and leave the screen as it was before executing onNavigateToB ?
Something like
Copy code
composable(A){
   // other stuff
   if(nonNullString){
       ScreenA( navHostController, nonNullString, ..., onNavigateToB = { navHostController.navigate(B)
nonNullString = null
} ) 
   }
   // after onNavigateToB executes, it recomposes and re-renders ScreenA 
}
composable(B){
   ScreenB( ... )
}
i
That kind of approach will mean your screen just immediately jump cuts into an entirely blank screen while the other screen fades in, so no, I don't think that's a viable option. It sounds like you might be mixing state (what ScreenA needs to be displayed, something that is required even during the crossfade) and events (the one time operation that triggers your navigate call). Maybe it would help to explain why onNavigateToB would do anything other than navigating to B
l
I agree that affecting ScreenA during navigation is not right, but what actually onNavigateB is doing is cleaning sharedPreferences and then navigating. What occurs is that ScreenA is using sharedPreferences to display some strings
and due recomposition, it ends up crashing since the sharedPrefs were cleared
i
Sounds like you should be clearing those in a DisposableEffect's onDispose, which is what actually signals the screen's last composition
l
I'll take a look into that! Thanks for helping! 🙏 🙏