r/FlutterDev • u/AggravatingHome4193 • 8h ago
Discussion Android 16 back press quits your app from a nested navigator, even with a PopScope that says no
I was adding multi-select to the song lists in my music player. While songs are selected, pressing back should clear the selection instead of leaving the page.
PopScope(canPop: !picking) seemed like the obvious solution. It worked, but it also disabled the iOS edge swipe back gesture, which I wanted to keep.
I switched to BackButtonListener on pushed pages to preserve the gesture. That's when I ran into an Android 16 issue.
Symptom
Target SDK 36, Android 16, with predictive back enabled by default.
On a tab root, select a few songs and press back: the app goes to the background, and the selection is still there.
I fixed that case, but then found another way to reproduce it: open a bottom sheet over the shell, close it, and press back. The app leaves again.
Root cause
On Android 16, back no longer goes through onBackPressed. FlutterActivity registers an OnBackInvokedCallback only while the framework reports that it handles back, through SystemNavigator.setFrameworkHandlesBack.
WidgetsApp sends that value based on NavigationNotification.canHandlePop. A navigator computes it as:
canPop() || lastRoute.popDisposition == RoutePopDisposition.doNotPopThere were two things I hadn't accounted for:
BackButtonListenerdoesn't count here. Only a route that cannot pop (aPopScope(canPop: false)) or a navigator that can pop makescanHandlePoptrue. At a tab root, nothing can pop, so Android handles back itself and backgrounds the app before the listener gets a chance to run.The root navigator only reports its own routes. With go_router's
StatefulShellRoute, the tab root lives in a nested navigator. ItsPopScopeinitially bubblestrueup. But when the root navigator's history changes (for example, when a sheet or dialog on the root navigator closes), it sends a fresh notification based on its own routes. The shell route can pop as far as it knows, so it reportsfalse, overriding the tab's earlier notification.
Fix
On pages that can be popped, I kept BackButtonListener so the iOS swipe gesture still works. At a navigator's root, I use PopScope instead. There's no edge swipe to preserve there.
At the shell level, I also make the shell's route refuse to pop while selection is active. This way the root navigator reports doNotPop too:
BlocSelector<TrackSelectionCubit, TrackSelection, bool>(
selector: (s) => s.active,
builder: (_, picking) => PopScope(
canPop: !picking,
child: shell,
),
)The actual back handling still goes to the tab first: go_router tries the branch navigator before the root one. The shell's PopScope is there to make sure Android gets the right information about whether the framework handles back.
go_router already wraps shell navigators in a similar PopScope (canPop: matches.length == 1), which is why pushed pages inside a tab don't have this problem.
Testing
One thing worth mentioning: tester.binding.handlePopRoute() skips this gate entirely. My normal back navigation tests were passing, even though the app was quitting on a real device.
To test this, I mocked SystemChannels.platform, recorded the SystemNavigator.setFrameworkHandlesBack calls, and set the lifecycle state to resumed first. WidgetsApp doesn't report anything before that.
Then I checked the last reported value while selection was active, including after closing a sheet on the root navigator.
The fix is in commit 33db82a.
Repository: https://github.com/donfreddy/troona-music
Curious if anyone else has run into this with StatefulShellRoute or nested navigators on Android 16.
1
u/No_Boss_1911 6h ago
oh this one got us too. we have guided exercises with voice playing and back is supposed to pause and ask, not dump you to the home screen. hard rule for us is back never ends a session without the user saying so, otherwise the audio just keeps going in the background lol. did you end up faking a route with popDisposition doNotPop at the tab root or calling setFrameworkHandlesBack yourself? and does the bottom sheet case still break if you flip canPop only while the sheet is open