grahammendick / navigation

Scene-Based Navigation for React and React Native
https://grahammendick.github.io/navigation/
Apache License 2.0
571 stars 40 forks source link

Supported predictive back gesture (Android) #781

Closed grahammendick closed 3 months ago

grahammendick commented 3 months ago

Updated Fragments to v1.7 and switched from Animations to Animators as per the Android guide. This brings predictive back gesture support to the FragmentManager back stack. Because the previous scene is now peekable on Android as well as iOS, removed the platform checks from the peekable logic. For example, rendering a fluently skipped scene in the background is necessary on Android, too.

The FragmentManager only supports predictive back on primary Fragments. A Fragment is only primary if it and all its ancestor Fragments are primary. The simplest way to do this was to create all stack Fragments as children of the Activity’s FragmentManager. Then don’t have to worry about the ancestors because they have none. But a FragmentManager can only have one primary Fragment at a time and switching the primary Fragment around, depending on which one was at the top, is difficult. Can’t just use onAttached/Detached to/from the window to move it around.

For example, consider a stack within a stack (not inside a modal - just one of the scenes in a stack renders a new stack). What happens after gesturing back to the beginning of the child stack? The child stack is still in the window but the gesture back should go to the parent instead.

So created the stack Fragments inside each other’s childFragmentManagers. In the Twitter sample, the top level stack is in the Activity’s FragmentManager and the child nested tab stacks are created in the top level stack’s childFragmentManager. That way both the parent stack and (one of) the nested stacks can both be primary Fragments at the same time. Now only have to shift the primary around between the sibling stacks, not between parent and child, and can use onAttached/Detached for that.

There’s some weirdness around the predictive back that couldn’t get to the bottom of.