How to get activity in compose

While the previous answer (which is ContextWrapper-aware) is indeed the correct one, I’d like to provide a more idiomatic implementation to copy-paste. fun Context.getActivity(): AppCompatActivity? = when (this) { is AppCompatActivity -> this is ContextWrapper -> baseContext.getActivity() else -> null } As ContextWrappers can’t possibly wrap each other significant number of times, recursion is fine … Read more

How are Android activities handled with Jetpack Compose and Compose Navigation?

The Compose application is designed to be used in a single-activity architecture with no fragments. You can still have multiple activities or fragments and use setContent in each of them, but in this case the transfer of data between activities falls on your shoulders. Use this approach if you’re adding new Compose screens to an … Read more

Why the view keeps flashing when using jetpack navigation with Compose?

Composite navigation recomposes both disappearing and appearing views during transition. This is the expected behavior. You’re calling navigate on each recomposition. Your problem lays in these lines: if (viewModel.isLoginSuccessful) { navController.navigate(Screen.AccountsScreen.route) { popUpTo(Screen.LoginScreen.route) { inclusive = true } } } You shouldn’t change state directly from view builders. In this case LaunchedEffect should be used: … Read more

Jetpack Compose – Order of Modifiers

There’s Layouts in Jetpack Compose codelab containing Layout modifiers under the hood step which explains the modifier order, see “Order matters” section. order matters when chaining modifiers as they’re applied to the composable they modify from earlier to later, meaning that the measurement and layout of the modifiers on the left will affect the modifier … Read more