https://kotlinlang.org logo
#compose
Title
# compose
m

Mehdi Haghgoo

11/09/2021, 2:58 PM
I have created a state holder containing some states of my app, e.g. RECORDING, PLAYING, etc. But, changes I make to the state do not invoke recomposition. Continued...
Here's the state holder:
Copy code
class AppState(
    val player: MediaPlayer,
    val recorder: MediaRecorder,
    var mediaState: MediaState,
    val scaffoldState: ScaffoldState,
    val navController: NavHostController,
    private val resources: Resources
    ){

    fun navigateToBottomBarRoute(route:String){
        navController.navigate(route){
            popUpTo(navController.graph.findStartDestination().id){
                saveState = true
            }
            restoreState = true
            launchSingleTop = true

        }
    }

    @OptIn(DelicateCoroutinesApi::class)
    fun showSnackbar(message: String){
        GlobalScope.launch {
            scaffoldState.snackbarHostState.showSnackbar(message, duration = SnackbarDuration.Long)
        }
    }

}
And i access the state as follows:
Copy code
@Composable
fun rememberAppState(
    player: MediaPlayer,
    recorder: MediaRecorder,
    mediaState: MediaState,
    scaffoldState: ScaffoldState,
    navController: NavHostController,
    resources: Resources = LocalContext.current.resources
) = remember(player, recorder, mediaState, scaffoldState, navController, resources){
       AppState(player, recorder, mediaState, scaffoldState, navController, resources)
   }
For some reason, when I change the value of a component e.g. mediaState, the composables using it are not recomposed, so my UI is not updated unless I navigate to another screen and come back to see the updated UI.
z

Zach Klippenstein (he/him) [MOD]

11/09/2021, 4:33 PM
Your mediaState property isn't backed by a MutableState so Compose has no way of knowing when it changes.
1
❤️ 1
5 Views