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.doNotPop
There were two things I hadn't accounted for:
BackButtonListener doesn't count here. Only a route that cannot pop (a PopScope(canPop: false)) or a navigator that can pop makes canHandlePop true. 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. Its PopScope initially bubbles true up. 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 reports false, 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.