What does Jetpack Compose remember actually do, how does it work under the hood?

remember – allows you to remember state from previous recompose invocation and just this. So, if you for instance randomize color at initial run. The randomized color is going to be calculated only once and later reused whenever re-compose is necessary. And hence, remember = store the value just in case recompose is called. Now, … Read more

Show custom alert dialog in Jetpack Compose

Starting from M3 1.1.0-alpha04 there is an AlertDialog composable function with a slot for content. val openDialog = remember { mutableStateOf(true) } if (openDialog.value) { androidx.compose.material3.AlertDialog( onDismissRequest = { // Dismiss the dialog when the user clicks outside the dialog or on the back // button. If you want to disable that functionality, simply use … Read more

When should I use Android Jetpack Compose Surface composable?

Surface composable makes the code easier as well as explicitly indicates that the code uses a material surface. Let’s see an example: Surface( color = MaterialTheme.colors.primarySurface, border = BorderStroke(1.dp, MaterialTheme.colors.secondary), shape = RoundedCornerShape(8.dp), elevation = 8.dp ) { Text( text = “example”, modifier = Modifier.padding(8.dp) ) } and the result: The same result can be … Read more

Exposed drop-down menu for jetpack compose

The M2 (starting from the version 1.1.0-alpha06) and M3 have the implementation of ExposedDropdownMenu based on ExposedDropdownMenuBox with TextField and DropdownMenu inside. Something like: val options = listOf(“Option 1”, “Option 2”, “Option 3”, “Option 4”, “Option 5”) var expanded by remember { mutableStateOf(false) } var selectedOptionText by remember { mutableStateOf(options[0]) } ExposedDropdownMenuBox( expanded = expanded, … Read more

How to share a viewmodel between two or more Jetpack composables inside a Compose NavGraph?

You can to pass your top viewModelStoreOwner to each destination directly passing to .viewModel() call, composable(“first”) in my example overriding LocalViewModelStoreOwner for the whole content, so each composable inside CompositionLocalProvider will have access to the same view models, composable(“second”) in my example val viewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) { “No ViewModelStoreOwner was provided via LocalViewModelStoreOwner” } val … Read more

Jetpack Compose Navigation loads screen infinitely

I re-implemented your posted code with 2 screens, HomeScreen and SettingScreen and stripped out some part of the UiState class and its usages. The issue is in your HomeScreen composable, not in the StateFlow emission. You have this mutableState val uiState by viewModel.uiState.collectAsStateWithLifecycle( initialValue = UiState.Speak ) that is being observed in one of your … Read more

How to pass id and application to a viewModel/viewModelFactory in Jetpack Compose?

To answer your question: you retrieve the Application from the LocalContext object: val context = LocalContext.current val application = context.applicationContext as Application However, when using Navigation Compose, you don’t need to manually pass any arguments to your ViewModel. Instead, you can utilize the built in support for SavedState in ViewModels and add a SavedStateHandle parameter … Read more

fillMaxSize modifier not working when combined with VerticalScroll in Jetpack Compose

verticalScroll wraps the height of the content, and can be stretched to very long, so the scope has Constraints.Infinity for maxHeight constraint. From fillMaxHeight documentation If the incoming maximum height is Constraints.Infinity this modifier will have no effect. That’s why you need to set height explicitly. Consider switching to LazyColumn(which has fillParentMaxHeight() for this exact … Read more