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

What is the equivalent of [NestedScrollView + RecyclerView] or [Nested RecyclerView (Recycler inside another recycler) in Jetpack compose

I think the best option, would be if the LazyVerticalGrid allows some sort of expand logic on each item, but looks like it’s not supported yet (beta-03). So I’m leaving here my solution using one single LazyColumn for the entire list and LazyRow for “My Books” section. LazyColumn( modifier = Modifier.fillMaxSize(), ) { // My … Read more

Jetpack Compose collapsing toolbar

Jetpack Compose implementation of Material Design 3 includes 4 types of Top App Bars (https://m3.material.io/components/top-app-bar/implementation): CenterAlignedTopAppBar SmallTopAppBar MediumTopAppBar LargeTopAppBar https://developer.android.com/reference/kotlin/androidx/compose/material3/package-summary They all have a scrollBehavior parameter, which can be used for collapsing the toolbar. There are 3 basic types of scroll behavior in the library: TopAppBarDefaults.pinnedScrollBehavior TopAppBarDefaults.enterAlwaysScrollBehavior TopAppBarDefaults.exitUntilCollapsedScrollBehavior https://developer.android.com/reference/kotlin/androidx/compose/material3/TopAppBarDefaults Note: This API is annotated as … Read more

Is there a way to increase a Composable size by chaining with another Modifier?

Changing Modifier size maybe possible with creating a Modifier such as SizeModifier. If anyone posts accurate answer that accomplishes task that way would be more than welcome. I solved this by first setting a requiredSize to have minimum dimensions for this Composable. @Composable fun TransformLayout( modifier: Modifier = Modifier, enabled: Boolean = true, handleRadius: Dp … Read more

How can I detect a click with the view behind a Jetpack Compose Button?

When button finds tap event, it marks it as consumed, which prevents other views from receiving it. This is done with consumeDownChange(), you can see detectTapAndPress method where this is done with Button here To override the default behaviour, you had to reimplement some of gesture tracking. List of changes comparing to system detectTapAndPress: I … Read more

How to handle one-shot operations in Jetpack Compose?

I prefer using SharedFlow for a job like this: class OneShotOperationViewModel : ViewModel() { private val _toastMessage = MutableSharedFlow<String>() val toastMessage = _toastMessage.asSharedFlow() fun sendMessage(message: String) { viewModelScope.launch { _toastMessage.emit(message) } } } @Composable fun TestScreen() { val context = LocalContext.current val viewModel = viewModel<OneShotOperationViewModel>() LaunchedEffect(Unit) { viewModel .toastMessage .collect { message -> Toast.makeText( context, … 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