androidCompositionBoundary: first inputs change disposes the content's state (factory and update use different setContent lambdas)
Maintainers usually reply within 4 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 50/100
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") }
}
}
- Launch and tap Count a few times (say, to 3).
- Tap Switch. Count resets to 0 and "Counter appeared" is logged again.
- 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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from skiptools/skip-ui
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 4 days
All issues in skiptools/skip-ui
Similar issues
-
AndroidAssetManager(env:peer:) in AssetURLProtocol.swift doesn't compile for Android on the Swift nightlyPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
good first issue help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
erkamyaman/unslop-anything#579 ·
Maintainers usually reply within 1 day
-
good first issue help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
erkamyaman/rust-anything#1311 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
bug priority:low status:queued
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
Maintainers usually reply within 1 day