Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dropping back stack retained state on Android Activity rotations #1063

Merged
merged 6 commits into from
Dec 20, 2023

Conversation

chrisbanes
Copy link
Contributor

@chrisbanes chrisbanes commented Dec 19, 2023

We store the RetainedStateRegistries for each back stack entry into an 'navigation content' RetainedStateRegistry. If we don't do this, those registries would be stored directly in the current LocalRetainedStateRegistry value, which will mostly likely be the continuityRetainedStateRegistry. On Android, that continuityRetainedStateRegistry will drop any 'unclaimed' values when the host Activity is recreated. Since records on the back stack aren't attached to composition, they can't claim their retained registries and thus we drop all of the state for the record.

Using this 'navigation content' registry means that it will be stored in the continuityRetainedStateRegistry instead, and any back stack record registries stored within the 'navigation content' registry. The difference is that NavigableCircuitContent will be attached to composition for the entire lifetime that we care about, and thus will be able to save/claim the 'navigation content' registry on recreations. Since any back stack registries are nested in this 'navigation content' registry, everything is saved/claimed correctly.

Added an Android instrumentation test to stop this regressing in the future.

Fixes #1046

@chrisbanes chrisbanes force-pushed the cb/retained-navigation-recreate branch from 0161ea8 to 187d090 Compare December 19, 2023 19:37
@chrisbanes chrisbanes changed the title Add Retained + Navigation + Recreate Android test Fix dropping back stack retained state on Android Activity rotations Dec 20, 2023
@chrisbanes chrisbanes marked this pull request as ready for review December 20, 2023 10:48
@chrisbanes chrisbanes force-pushed the cb/retained-navigation-recreate branch from 63872fe to cabf667 Compare December 20, 2023 10:53
We store the RetainedStateRegistries for each back stack entry into an
'navigation content' RetainedStateRegistry. If we don't do this, those
registries would be stored directly in the current
LocalRetainedStateRegistry value, which will mostly likely be the
continuityRetainedStateRegistry. On Android, that
continuityRetainedStateRegistry will drop any 'unclaimed' values when
the host Activity is recreated. Since records on the back stack aren't
attached to composition, they can't claim their retained registries and
thus we drop all of the state for the record. See slackhq#1046.

Using this 'navigation content' registry means that _it_ will be stored
in the continuityRetainedStateRegistry instead, and any back stack
record registries stored within the 'navigation content' registry. The
difference is that NavigableCircuitContent will be attached to
composition for the entire lifetime that we care about, and thus will
be able to save/claim the 'navigation content' registry on recreations.
Since any back stack registries are nested in this 'navigation content'
registry, everything is saved/claimed correctly.
@chrisbanes chrisbanes force-pushed the cb/retained-navigation-recreate branch from 7b2c8ff to 5e40c70 Compare December 20, 2023 11:04
Copy link
Collaborator

@ZacSweers ZacSweers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome. We currently don't run any instrumentation tests on CI (we'd moved them all to run in robolectric), but I can work on adding that CI support back in a separate PR if you think these couldn't also move to Robolectric

if (backstack.size > 0) {
if (backstack.isEmpty) return

/*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole doc is

giphy

@chrisbanes
Copy link
Contributor Author

chrisbanes commented Dec 20, 2023

I tried porting this to Robolectric but couldn't get the original issue to fail. Rotations/recreations are tricky to simulate so not surprising tbh.

@ZacSweers ZacSweers added this pull request to the merge queue Dec 20, 2023
Merged via the queue into slackhq:main with commit ca90b4f Dec 20, 2023
3 checks passed
@ZacSweers ZacSweers deleted the cb/retained-navigation-recreate branch December 20, 2023 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rememberRetained values in backstack cleared after configuration change.
2 participants