How to navigate from a composable to an activity or a fragment in Jetpack Compose?

In newer version of compose use LocalContext. In older versions (1.0.0-alpha08 and before) use AmbientContext: @Composable fun MainScreen() { val context = LocalContext.current Button(onClick = { context.startActivity(Intent(context, ListActivity::class.java)) }) { Text(text = “Show List”) } }

Orientation on Jetpack Compose

We can use LocalConfiguration to listen for the orientation changes @Composable fun ConfigChangeExample() { val configuration = LocalConfiguration.current when (configuration.orientation) { Configuration.ORIENTATION_LANDSCAPE -> { Text(“Landscape”) } else -> { Text(“Portrait”) } } } Note: This will not help to listen to the configuration change, This will just help to get the current Configuration.

Can no longer view Jetpack Compose Previews. Failed to instantiate one or more classes (ComposeViewAdapter)

It is a known bug: https://issuetracker.google.com/issues/227767363 Google is currently working on a fix but there is already a workaround: add these dependencies to every module where you use the Compose preview: debugImplementation “androidx.customview:customview:1.2.0-alpha01” debugImplementation “androidx.customview:customview-poolingcontainer:1.0.0-alpha01” If you have a common core/base/ui module, you can add it there and use Api instead of Implementation to avoid … Read more

java.lang.IllegalStateException when using State in Android Jetpack Compose

Three ways this can be resolved are To call the method in a launched effect block in your composable Or set the Context to Dispatchers.Main when setting value of the mutableState using withContext(Dispatchers.Main) Or change the mutable state in viewModel to mutableState flow and use collectAsState() in composable to collect it as a state.

What do the @Stable and @Immutable annotations mean in Jetpack Compose?

The definition @Immutable is an annotation to tell Compose compiler that this object is immutable for optimization, so without using it, there will be unnecessary re-composition that might get triggered. @Stable is another annotation to tell the Compose compiler that this object might change, but when it changes, Compose runtime will be notified. It might … 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