androidCompositionBoundary: first inputs change disposes the content's state (factory and update use different setContent lambdas)

Open
#614 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 4 days

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
50/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
android, swift
Domain
mobile

Research direction

The two setContent calls are in Sources/SkipUI/SkipUI/Compose/AndroidCompositionBoundary.swift, inside the factory and update blocks of Render(context:). Start by reading the doc-comment contract for androidCompositionBoundary(id:inputs:), then reproduce with the Shell/Counter snippet and onAppear logging to confirm the remount on the first inputs change. Done means that first change preserves the bounded content's @State and does not remount, while changing id still disposes it.

Written by the indexing model from the issue text.

Description

Summary

androidCompositionBoundary(id:inputs:) loses all of its content's state the first time inputs changes after the boundary is created. Later inputs changes keep state. The doc comment says the opposite ("Changing inputs updates content inside the existing container; changing id disposes it and creates a new one").

Where

Sources/SkipUI/SkipUI/Compose/AndroidCompositionBoundary.swift, Render(context:) (same on 1.61.0 and on main today):

AndroidView(
    factory: { androidContext in
        return AndroidCompositionBoundaryComposeView(context: androidContext, parentCompositionContext: parentCompositionContext) {
            storage.content.Compose(context: childContext)      // lambda A
        }
    },
    modifier: context.modifier,
    update: { composeView in
        guard storage.inputs != inputs else { return }
        storage.inputs = inputs
        storage.content = preparedProjection ?? content()
        composeView.setContent {
            storage.content.Compose(context: childContext)      // lambda B
        }
    }
)

The factory installs the content with one composable lambda and update replaces it with a different one, written at a different source location. Compose keys a composable lambda's group by where it is written, so the first setContent from update swaps the root group and disposes everything remembered under it: @State, navigation stacks, remember, running .tasks. Every later update passes a lambda from the same location as the previous one, so state survives from then on. That is why it looks intermittent.

Repro

struct Shell: View {
    @State var tab = 0
    var body: some View {
        VStack {
            Button("Switch") { tab = (tab + 1) % 2 }
            Counter()
                .androidCompositionBoundary(id: "counter", inputs: "\(tab)")
        }
    }
}

struct Counter: View {
    @State var count = 0
    var body: some View {
        Button("Count: \(count)") { count += 1 }
            .onAppear { print("Counter appeared") }
    }
}
  1. Launch and tap Count a few times (say, to 3).
  2. Tap Switch. Count resets to 0 and "Counter appeared" is logged again.
  3. Tap Count to 3 again, then Switch twice more. The count stays at 3.

Observed on a Skip Fuse app (release build, Android API 37 emulator, skip-ui 1.61.0). In a real app this showed up as tabs losing their state on the first tab switch after launch: an open detail screen popped, editor state reset. We confirmed it with onAppear/onDisappear logging on the bounded view. A remount was logged exactly on the first inputs change after each boundary creation and never on later ones.

Suggested fix

Install one lambda and never replace it: keep the content in Compose state that lambda A reads, and have update write to that state instead of calling setContent. For example, storage could hold a mutableStateOf<any View>. A change then recomposes inside the existing composition instead of starting a new root. Reusing one stored lambda instance for both setContent calls would probably also work, but the state-driven version seems closer to the documented contract.

Workaround

Pass no inputs (keep them constant) and have the bounded content read changing values from an @Observable inside its own composition, so update never calls setContent.

Dominant language
Swift
Stars
334
Forks
95
Avg merge
2d 10h
Merged PRs (30d)
8

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from skiptools/skip-ui

All issues in skiptools/skip-ui

Similar issues

More Swift issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.