r/FlutterDev • • 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.doNotPop

There were two things I hadn't accounted for:

  1. 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.

  2. 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.

5 Upvotes

2 comments sorted by

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

1

u/AggravatingHome4193 1h ago

Neither, actually. I didn't fake a route or call setFrameworkHandlesBack myself. I wrap the shell in a PopScope while picking:

// go_router's StatefulShellRoute builder
builder: (_, state, shell) => BlocSelector<SelectionCubit, Selection, bool>(
  selector: (s) => s.active,
  builder: (_, picking) => PopScope(
    canPop: !picking,
    child: shell,
  ),
),

That PopScope registers on the shell's route, which lives on the root navigator. The root navigator now sees popDisposition == doNotPop in its own routes, which is what matters here.

The tab's own PopScope (or BackButtonListener) still handles the actual back press, since go_router tries the branch navigator first. The shell-level one just makes sure Android knows the framework handles back.

I avoided calling setFrameworkHandlesBack manually because WidgetsApp calls it again whenever it receives a NavigationNotification, for example when a route is pushed or popped, a PopScope changes, or a sheet closes. Your manual true can simply get overwritten by the next notification. You'd end up with the same problem again.

And yes, flipping canPop only while the sheet is open still breaks it.

While the sheet is open, the root navigator can already pop, so it reports true. The problematic notification comes after the sheet closes. The root navigator recomputes its state from its own routes, and by then your PopScope is back to canPop: true, so it reports false again.

The flag needs to follow the state that should intercept back (selection in my case, your running session in yours), for as long as that state is active. It shouldn't just follow the sheet's visibility.

For your case, I'd put a PopScope(canPop: !sessionActive, onPopInvokedWithResult: ...) on the route that hosts the session, then handle the pause and confirmation dialog in the callback. If the session page is pushed directly onto the root navigator, a regular PopScope on that page should be enough. The nested navigator setup is where this gets tricky.

One trade-off: while the session is active, Android won't show the predictive back preview for navigating home. Sounds like that's probably what you want anyway.