Android compose patterns

From FerroxLabs/murage · View interactive version

Android compose patterns is an AI agent skill from the GitHub repository FerroxLabs/murage. Guides expert-level android compose patterns implementation: kotlin and frameworks decision frameworks, production-ready patterns, and concrete templates for android compose patterns workflows. Use when the user asks about android compose patterns, android compose patterns configuration, or mobile b

How to use this skill

Skill file

Android Compose Patterns

When to Use

Use this skill when:

Do NOT use this skill when:


Process

1. Establish the Compose Architecture Tier

Before writing a single composable, identify which architectural pattern fits the project:

2. Define the UiState Contract

The UiState data class is the central contract between ViewModel and UI:

3. Design the Composable Hierarchy

Structure composables in three tiers to maximize reusability and testability:

Key design rules for the hierarchy:

4. Apply State Hoisting and `remember` Correctly

State hoisting and memoization are the two most commonly misused Compose concepts:

5. Handle Side Effects with the Correct Effect API

Compose provides four primary side-effect APIs -- choosing the wrong one causes bugs ranging from infinite loops to missed events:

6. Optimize Recomposition

Uncontrolled recomposition is the most common Compose performance problem in production:

7. Implement Navigation with Type Safety

Compose Navigation requires deliberate structure to remain maintainable at scale:

8. Apply Theming and Design System Patterns


Output Format

When responding to a user request about Compose patterns, structure output as follows:

## Compose Pattern: [Pattern Name]

### Context & Applicability
- Applies when: [specific scenario]
- Avoid when: [specific counter-scenario]
- Complexity: [Low / Medium / High]

### UiState Definition

// Annotate for Compose compiler stability inference

@Immutable

data class [Screen]UiState(

val isLoading: Boolean = false,

val [dataField]: [Type] = [default],

val errorMessage: String? = null

)

sealed class [Screen]UiEffect {

data class NavigateTo(val route: [RouteType]) : [Screen]UiEffect()

data class ShowSnackbar(val message: String) : [Screen]UiEffect()

}


### ViewModel Structure

@HiltViewModel

class [Screen]ViewModel @Inject constructor(

private val [dependency]: [DependencyType],

savedStateHandle: SavedStateHandle

) : ViewModel() {

private val _uiState = MutableStateFlow([Screen]UiState())

val uiState: StateFlow<[Screen]UiState> = _uiState.asStateFlow()

private val _uiEffect = Channel<[Screen]UiEffect>(Channel.BUFFERED)

val uiEffect: Flow<[Screen]UiEffect> = _uiEffect.receiveAsFlow()

fun onEvent(event: [Screen]Event) { /* ... */ }

}


### Screen Composable

@Composable

fun [Screen]Screen(

viewModel: [Screen]ViewModel = hiltViewModel(),

onNavigateBack: () -> Unit

) {

val uiState by viewModel.uiState.collectAsStateWithLifecycle()

val context = LocalContext.current

LaunchedEffect(Unit) {

viewModel.uiEffect.collect { effect ->

when (effect) {

is [Screen]UiEffect.NavigateTo -> { /* handle */ }

is [Screen]UiEffect.ShowSnackbar -> { /* handle */ }

}

}

}

[Screen]Content(

uiState = uiState,

onEvent = viewModel::onEvent

)

}


### Content Composable (Testable)

@Composable

fun [Screen]Content(

uiState: [Screen]UiState,

onEvent: ([Screen]Event) -> Unit,

modifier: Modifier = Modifier

) {

// Pure UI -- no ViewModel, no DI

}


### Recomposition Risk Assessment
| Composable | State Read Frequency | Optimization Applied |
|------------|---------------------|----------------------|
| [Name] | [High/Medium/Low] | [technique] |

### Test Coverage Targets
| Layer | Test Type | Tool |
|-------|-----------|------|
| ViewModel | Unit | JUnit5 + Turbine |
| Content composable | Compose UI test | ComposeTestRule |
| Screen composable | Integration | Hilt test + ComposeTestRule |
| Navigation flow | E2E | UiAutomator / Espresso |

### Known Trade-offs
- [trade-off description and mitigation]

Rules

  1. Never hoist state higher than necessary. Hoisting TextField value into the ViewModel when only one composable uses it adds unnecessary ViewModel complexity and causes every ViewModel state observer to recompose on each keystroke. Keep transient UI state local.
  1. Never pass lambdas to deeply nested composables without remember stabilization. A non-remembered lambda captured from a recomposing scope creates a new instance on every parent recomposition. Use remember(viewModel) { { param -> viewModel.action(param) } } or reference bound method references directly.
  1. Never use @Stable or @Immutable on classes you do not control. Annotating third-party or domain model classes from outside your module lies to the Compose compiler and produces hard-to-diagnose stale UI bugs. Instead, create dedicated UI model classes in the ui layer.
  1. Always use collectAsStateWithLifecycle() instead of collectAsState() for StateFlow in screen composables. collectAsState() continues collecting when the app is backgrounded, wasting CPU and battery. collectAsStateWithLifecycle() from lifecycle-runtime-compose automatically pauses collection when the lifecycle drops below STARTED.
  1. Never deliver one-shot events (navigation, toasts, dialogs) via StateFlow. StateFlow replays the last value on new collectors, causing navigation events to re-fire after configuration changes. Use Channel(Channel.BUFFERED).receiveAsFlow() for events that should be consumed exactly once.
  1. Never call remember without keys when the remembered object depends on a parameter. remember { expensiveComputation(userId) } will return a stale result when userId changes because the key is omitted. Always write remember(userId) { expensiveComputation(userId) }.
  1. Never read State inside Modifier.padding(), Modifier.size(), or layout modifiers when the value changes at high frequency (e.g., driven by animation or gesture). Layout-phase state reads trigger relayout on every change. Move such reads to Modifier.graphicsLayer { } or Modifier.offset { } (the lambda variant) which execute in the draw phase without triggering recomposition or relayout.
  1. Always provide contentDescription for interactive composable elements. Any Image, Icon, Button, or custom clickable composable must have a non-null contentDescription for accessibility. Use contentDescription = null only for purely decorative elements -- document the reason in a comment.
  1. Never use GlobalScope, lifecycleScope, or MainScope inside composable functions. Use rememberCoroutineScope() for event-driven coroutines inside composables, or launch from the ViewModel using viewModelScope. External scopes leak the composable or attach to the wrong lifecycle.
  1. Always run Compose Compiler Metrics on CI before releasing a new screen. The metrics report identifies which composables are skippable, which parameters are unstable, and which classes trigger unnecessary recomposition. Set a threshold: no screen composable tree should have more than 20% non-skippable composables without documented justification.

Edge Cases

Migrating an Existing Fragment to Compose Incrementally

When adding Compose to a screen backed by a Fragment, use ComposeView inside the Fragment's onCreateView. Set the ViewCompositionStrategy to ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed to align the Compose lifecycle with the Fragment view lifecycle (not the Fragment lifecycle itself -- using the wrong strategy causes double-subscription bugs with collectAsStateWithLifecycle). The Fragment ViewModel is shared using viewModels() and passed into the ComposeView content lambda. Do not create a second ViewModel for the Compose content -- maintain a single source of truth.

LazyColumn with Heterogeneous Item Types

When LazyColumn renders multiple item types (headers, regular items, ads, loading indicators), define a sealed class for list item types and use LazyListScope.items(items, key = { it.stableId }, contentType = { it.contentType }). The contentType parameter allows Compose to recycle composition nodes across items of the same type, significantly improving scroll performance. Without contentType, Compose treats every item slot as potentially different and performs full recomposition on scroll. Expected performance improvement: 20-40% reduction in frame time for heterogeneous lists longer than 50 items.

State Restoration After Process Death

rememberSaveable using default Saver handles primitive types and Parcelable classes automatically. For non-Parcelable state (e.g., a complex filter configuration represented as a Kotlin data class), implement a custom Saver using mapSaver or listSaver. If the state object is too large for the Bundle (limit is approximately 500KB -- exceeding this causes TransactionTooLargeException), store the state in the ViewModel's SavedStateHandle instead, which uses the same Bundle mechanism but with automatic key namespacing. Never store bitmaps, large lists, or network responses in rememberSaveable.

Shared Element Transitions Between Screens

Compose 1.7+ (in androidx.compose.animation) introduces the SharedTransitionLayout and SharedTransitionScope API. Wrap the NavHost in SharedTransitionLayout. Each composable that participates in the transition receives AnimatedVisibilityScope from the navigation lambda and SharedTransitionScope from the layout. Apply Modifier.sharedElement(rememberSharedContentState(key = "hero-image-${item.id}"), animatedVisibilityScope) to matching elements in both source and destination screens. Ensure the shared element key is deterministic and unique -- use item IDs, not positional indices. For the transition to be smooth, the shared element must have identical Modifier.size() or fillMaxSize behavior on both screens, or use Modifier.sharedBounds for elements that change size.

Handling Keyboard and IME Interactions

Soft keyboard appearance causes window resizing or panning that composables must handle explicitly. Use WindowCompat.setDecorFitsSystemWindows(window, false) in the Activity and apply Modifier.imePadding() to the root scrollable container. For screens with a TextField near the bottom of the screen, wrap content in a Column with Modifier.verticalScroll(rememberScrollState()).imePadding(). For LazyColumn, use Modifier.imeNestedScroll() to allow the list to scroll in response to IME changes. Test keyboard handling on API 30+ (where the new WindowInsets IME animation API is available) and API 29 and below separately -- behavior differs significantly.

Compose in Multi-Module Projects

In a multi-module project, place shared design system composables (AppTheme, spacing tokens, typography, reusable components) in a dedicated :designsystem or :ui-components module. Feature modules depend on :designsystem but NOT on each other. The :app module assembles the navigation graph by depending on all feature modules. Avoid placing composables in the :domain or :data layers -- these layers must remain platform-agnostic and free of Compose dependencies to preserve testability and enable potential multiplatform sharing. The Compose compiler plugin must be applied in every module that contains @Composable functions -- a missing plugin in a module produces cryptic errors about "composable functions called from non-composable context".

Testing Composables with Asynchronous State

When testing a composable that displays data loaded asynchronously, use composeTestRule.waitUntilAtLeastOneExists(hasText("Expected Content"), timeoutMillis = 5000) from compose-test 1.5.0+ rather than adding arbitrary delay() calls or using composeTestRule.mainClock.advanceTimeBy(). For ViewModel-backed tests using real coroutines, inject a TestCoroutineDispatcher and use runTest with advanceUntilIdle() to ensure all pending coroutines complete before asserting UI state. Use the Turbine library for asserting StateFlow and Channel emissions in sequence -- it provides awaitItem(), expectNoEvents(), and timeout controls that are far more reliable than manual collect + job.cancel() patterns.


Example

Input: "I'm building a product detail screen in Jetpack Compose. The screen loads product data from a repository, shows a loading spinner, allows the user to add the item to a cart, and navigates back on success. My team uses Hilt and follows MVVM. What's the complete pattern?"

Output:

Compose Pattern: Product Detail Screen (MVVM + Hilt + UDF)

Context & Applicability:

This pattern applies to any screen that loads remote data, handles user actions with side effects, and triggers navigation. It uses a flat UiState data class (not sealed) because the screen can show loading and content simultaneously (loading overlay during "Add to Cart"), and uses a Channel-based effect for one-shot navigation and snackbar events.


Step 1: Define the UiState and UiEffect contracts
@Immutable
data class ProductDetailUiState(
    val isLoading: Boolean = true,
    val product: ProductUiModel? = null,
    val isAddingToCart: Boolean = false,
    val errorMessage: String? = null
)

// UI model -- separate from domain model, lives in :feature-product module
@Immutable
data class ProductUiModel(
    val id: String,
    val name: String,
    val description: String,
    val priceFormatted: String,    // pre-formatted in ViewModel, not composable
    val imageUrl: String,
    val isInStock: Boolean
)

sealed class ProductDetailUiEffect {
    object NavigateBack : ProductDetailUiEffect()
    data class ShowSnackbar(val message: String) : ProductDetailUiEffect()
}

sealed class ProductDetailEvent {
    object AddToCart : ProductDetailEvent()
    object RetryLoad : ProductDetailEvent()
    object NavigateBack : ProductDetailEvent()
}

Step 2: Implement the ViewModel
@HiltViewModel
class ProductDetailViewModel @Inject constructor(
    private val getProductUseCase: GetProductUseCase,
    private val addToCartUseCase: AddToCartUseCase,
    savedStateHandle: SavedStateHandle
) : ViewModel() {

    // Route argument -- type-safe via SavedStateHandle with navigation-compose 2.8+
    private val productId: String = checkNotNull(savedStateHandle["productId"])

    private val _uiState = MutableStateFlow(ProductDetailUiState())
    val uiState: StateFlow<ProductDetailUiState> = _uiState.asStateFlow()

    // Buffered channel -- survives brief UI absence without dropping events
    private val _uiEffect = Channel<ProductDetailUiEffect>(Channel.BUFFERED)
    val uiEffect: Flow<ProductDetailUiEffect> = _uiEffect.receiveAsFlow()

    init {
        loadProduct()
    }

    fun onEvent(event: ProductDetailEvent) {
        when (event) {
            is ProductDetailEvent.AddToCart -> addToCart()
            is ProductDetailEvent.RetryLoad -> loadProduct()
            is ProductDetailEvent.NavigateBack -> {
                viewModelScope.launch {
                    _uiEffect.send(ProductDetailUiEffect.NavigateBack)
                }
            }
        }
    }

    private fun loadProduct() {
        viewModelScope.launch {
            _uiState.update { it.copy(isLoading = true, errorMessage = null) }
            getProductUseCase(productId)
                .onSuccess { product ->
                    _uiState.update {
                        it.copy(
                            isLoading = false,
                            product = product.toUiModel()  // mapping in ViewModel
                        )
                    }
                }
                .onFailure { error ->
                    _uiState.update {
                        it.copy(
                            isLoading = false,
                            errorMessage = error.toUserMessage()  // extension fun maps exceptions
                        )
                    }
                }
        }
    }

    private fun addToCart() {
        val product = _uiState.value.product ?: return
        viewModelScope.launch {
            _uiState.update { it.copy(isAddingToCart = true) }
            addToCartUseCase(product.id)
                .onSuccess {
                    _uiEffect.send(
                        ProductDetailUiEffect.ShowSnackbar("${product.name} added to cart")
                    )
                    _uiEffect.send(ProductDetailUiEffect.NavigateBack)
                }
                .onFailure { error ->
                    _uiState.update {
                        it.copy(
                            isAddingToCart = false,
                            errorMessage = error.toUserMessage()
                        )
                    }
                }
        }
    }
}

Step 3: Implement the Screen Composable (ViewModel boundary)
@Composable
fun ProductDetailScreen(
    onNavigateBack: () -> Unit,
    viewModel: ProductDetailViewModel = hiltViewModel()
) {
    val uiState by viewModel.uiState.collectAsStateWithLifecycle()
    val snackbarHostState = remember { SnackbarHostState() }

    // Effect handling -- one-shot events consumed here, never in Content
    LaunchedEffect(Unit) {
        viewModel.uiEffect.collect { effect ->
            when (effect) {
                is ProductDetailUiEffect.NavigateBack -> onNavigateBack()
                is ProductDetailUiEffect.ShowSnackbar -> {
                    snackbarHostState.showSnackbar(
                        message = effect.message,
                        duration = SnackbarDuration.Short
                    )
                }
            }
        }
    }

    ProductDetailContent(
        uiState = uiState,
        snackbarHostState = snackbarHostState,
        onEvent = viewModel::onEvent
    )
}

Step 4: Implement the Content Composable (fully testable)
@Composable
fun ProductDetailContent(
    uiState: ProductDetailUiState,
    snackbarHostState: SnackbarHostState,
    onEvent: (ProductDetailEvent) -> Unit,
    modifier: Modifier = Modifier
) {
    Scaffold(
        modifier = modifier,
        snackbarHost = { SnackbarHost(snackbarHostState) },
        topBar = {
            TopAppBar(
                title = { Text(uiState.product?.name ?: "") },
                navigationIcon = {
                    IconButton(
                        onClick = { onEvent(ProductDetailEvent.NavigateBack) }
                    ) {
                        Icon(
                            imageVector = Icons.AutoMirrored.Filled.ArrowBack,
                            contentDescription = "Navigate back"
                        )
                    }
                }
            )
        },
        bottomBar = {
            uiState.product?.let { product ->
                AddToCartBar(
                    price = product.priceFormatted,
                    isInStock = product.isInStock,
                    isLoading = uiState.isAddingToCart,
                    onAddToCart = { onEvent(ProductDetailEvent.AddToCart) }
                )
            }
        }
    ) { innerPadding ->
        Box(
            modifier = Modifier
                .fillMaxSize()
                .padding(innerPadding)
        ) {
            when {
                uiState.isLoading && uiState.product == null -> {
                    // Initial loading -- full screen spinner
                    CircularProgressIndicator(
                        modifier = Modifier.align(Alignment.Center)
                    )
                }
                uiState.errorMessage != null && uiState.product == null -> {
                    // Full screen error with retry
                    ErrorContent(
                        message = uiState.errorMessage,
                        onRetry = { onEvent(ProductDetailEvent.RetryLoad) },
                        modifier = Modifier.align(Alignment.Center)
                    )
                }
                uiState.product != null -> {
                    ProductBody(
                        product = uiState.product,
                        modifier = Modifier.fillMaxSize()
                    )
                    // Inline error -- product is visible but an action failed
                    uiState.errorMessage?.let { error ->
                        Text(
                            text = error,
                            color = MaterialTheme.colorScheme.error,
                            style = MaterialTheme.typography.bodySmall,
                            modifier = Modifier
                                .align(Alignment.BottomCenter)
                                .padding(MaterialTheme.spacing.md)
                        )
                    }
                }
            }
        }
    }
}

Step 5: Recomposition Risk Assessment

| Composable | State Read Frequency | Optimization Applied |

|---|---|---|

| ProductDetailScreen | Low -- only effect collection | collectAsStateWithLifecycle stops collection when backgrounded |

| ProductDetailContent | Medium -- state snapshot | @Immutable on UiState enables skipping when reference is equal |

| AddToCartBar | Low -- isLoading toggle | Receives primitive + lambda; lambda uses ::onEvent method ref, stable across recompositions |

| ProductBody | Very Low -- static after load | Receives @Immutable ProductUiModel; skipped entirely after first composition |

| TopAppBar title | Low -- product name only | Isolated read; only this composable recomposes when name changes |


Step 6: Test Coverage
// ViewModel unit test with Turbine
@OptIn(ExperimentalCoroutinesApi::class)
class ProductDetailViewModelTest {

    @get:Rule
    val mainDispatcherRule = MainDispatcherRule()  // replaces Main dispatcher with TestDispatcher

    private val getProductUseCase: GetProductUseCase = mockk()
    private val addToCartUseCase: AddToCartUseCase = mockk()
    private val savedStateHandle = SavedStateHandle(mapOf("productId" to "prod-123"))

    private lateinit var viewModel: ProductDetailViewModel

    @Test
    fun `loading product updates state to content on success`() = runTest {
        coEvery { getProductUseCase("prod-123") } returns Result.success(fakeProduct)
        viewModel = ProductDetailViewModel(getProductUseCase, addToCartUseCase, savedStateHandle)

        viewModel.uiState.test {
            val loading = awaitItem()
            assertThat(loading.isLoading).isTrue()
            val loaded = awaitItem()
            assertThat(loaded.isLoading).isFalse()
            assertThat(loaded.product?.id).isEqualTo("prod-123")
            cancelAndIgnoreRemainingEvents()
        }
    }

    @Test
    fun `add to cart success sends NavigateBack effect`() = runTest {
        coEvery { getProductUseCase("prod-123") } returns Result.success(fakeProduct)
        coEvery { addToCartUseCase("prod-123") } returns Result.success(Unit)
        viewModel = ProductDetailViewModel(getProductUseCase, addToCartUseCase, savedStateHandle)
        advanceUntilIdle()

        viewModel.uiEffect.test {
            viewModel.onEvent(ProductDetailEvent.AddToCart)
            assertThat(awaitItem()).isInstanceOf(ProductDetailUiEffect.ShowSnackbar::class.java)
            assertThat(awaitItem()).isEqualTo(ProductDetailUiEffect.NavigateBack)
            cancelAndIgnoreRemainingEvents()
        }
    }
}

// Content composable UI test
class ProductDetailContentTest {

    @get:Rule
    val composeTestRule = createComposeRule()

    @Test
    fun `add to cart button disabled when not in stock`() {
        composeTestRule.setContent {
            AppTheme {
                ProductDetailContent(
                    uiState = ProductDetailUiState(
                        isLoading = false,
                        product = fakeProductUiModel.copy(isInStock = false)
                    ),
                    snackbarHostState = remember { SnackbarHostState() },
                    onEvent = {}
                )
            }
        }
        composeTestRule
            .onNodeWithContentDescription("Add to cart")
            .assertIsNotEnabled()
    }

    @Test
    fun `retry button shown on full screen error`() {
        composeTestRule.setContent {
            AppTheme {
                ProductDetailContent(
                    uiState = ProductDetailUiState(
                        isLoading = false,
                        product = null,
                        errorMessage = "Failed to load product"
                    ),
                    snackbarHostState = remember { SnackbarHostState() },
                    onEvent = {}
                )
            }
        }
        composeTestRule
            .onNodeWithText("Retry")
            .assertIsDisplayed()
    }
}

Step 7: Navigation graph wiring
// In feature module's NavGraph extension
fun NavGraphBuilder.productGraph(
    navController: NavController
) {
    composable<ProductDetailRoute> {
        ProductDetailScreen(
            onNavigateBack = { navController.popBackStack() }
        )
    }
}

@Serializable
data class ProductDetailRoute(val productId: String)

// Navigating to the screen
navController.navigate(ProductDetailRoute(productId = item.id))

Key decisions made in this pattern and why:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.