Skip to content

Predictive back can close app when navigation stack is not empty #145290

Open

Description

@Geara0

Steps to reproduce

  1. add android:enableOnBackInvokedCallback="true" to main/AndroidManifest.xml
  2. run the code below
  3. go to /home/second by tapping IconButton
  4. open modalBottomSheet by tapping IconButton
  5. swipe to go back -> modalBottomSheet closes
  6. swipe to go back -> app exits

Expected results

after step 6 we should be at /home page
(and if there is no android:enableOnBackInvokedCallback="true" - bug doesn't appear)

Actual results

after step 6 we go to desktop

Code sample

Code sample
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    final dispatcher = BackDispatcher();
    return MaterialApp.router(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      // routerConfig: router,
      routerDelegate: router.routerDelegate,
      routeInformationProvider: router.routeInformationProvider,
      routeInformationParser: router.routeInformationParser,
      backButtonDispatcher: dispatcher,
    );
  }
}

class BackDispatcher extends RootBackButtonDispatcher {
  @override
  Future<bool> didPopRoute() async {
    if (modalStack.isNotEmpty) {
      debugPrint('popping modal');
      Navigator.of(navKey.currentContext!).pop();
      return true;
    }

    return super.didPopRoute();
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('home page'),
      ),
      body: Center(
        child: IconButton(
          onPressed: () => context.go('/home/second'),
          icon: const Icon(Icons.link),
        ),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  const SecondPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('second page'),
      ),
      body: Center(
        child: IconButton(
          onPressed: () {
            showModalBottomSheet(
              context: context,
              useRootNavigator: true,
              builder: (context) {
                return Container(
                  width: 100,
                  height: 100,
                  color: Colors.red,
                );
              },
            );
          },
          icon: const Icon(Icons.slideshow),
        ),
      ),
    );
  }
}

class Layout extends StatelessWidget {
  const Layout(
    this.shell, {
    super.key,
  });

  final StatefulNavigationShell shell;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Expanded(child: shell),
        Container(
          height: 10,
          color: Colors.blueGrey,
        ),
      ],
    );
  }
}

final modalStack = <Route>[];

class MyObserver extends RouteObserver {
  @override
  void didPush(Route route, Route? previousRoute) {
    if (previousRoute == null) return;
    debugPrint('!!! didPush ${route.runtimeType}');
    modalStack.add(route);
    super.didPush(route, previousRoute);
  }

  @override
  void didPop(Route route, Route? previousRoute) {
    debugPrint('!!! didPop ${route.runtimeType}');
    modalStack.removeLast();
    super.didPop(route, previousRoute);
  }
}

final navKey = GlobalKey<NavigatorState>();
GoRouter router = GoRouter(
  debugLogDiagnostics: true,
  observers: [MyObserver()],
  initialLocation: '/home',
  navigatorKey: navKey,
  routes: [
    GoRoute(
      path: '/',
      redirect: (_, state) {
        return state.fullPath == '/' ? '/home' : null;
      },
      routes: [
        StatefulShellRoute.indexedStack(
          builder: (context, state, shell) => Layout(shell),
          branches: [
            StatefulShellBranch(
              routes: [
                GoRoute(
                  path: 'home',
                  builder: (context, _) => const MyHomePage(),
                  routes: [
                    GoRoute(
                      path: 'second',
                      builder: (context, _) => const SecondPage(),
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
      ],
    ),
  ],
);

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.19.3, on macOS 14.3.1 23D60 darwin-arm64, locale en-RU)
    • Flutter version 3.19.3 on channel stable at /opt/homebrew/Caskroom/flutter/3.10.4/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ba39319843 (10 days ago), 2024-03-07 15:22:21 -0600
    • Engine revision 2e4ba9c6fb
    • Dart version 3.3.1
    • DevTools version 2.31.1

[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/geara0/Library/Android/sdk
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.1)
    • Android Studio at /Users/geara0/Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

[✓] IntelliJ IDEA Ultimate Edition (version 2023.1.1)
    • IntelliJ at /Users/geara0/Applications/IntelliJ IDEA Ultimate.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.87.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.84.0

[✓] Connected device (4 available)
    • M2101K6G (mobile)           • 79a46fc6      • android-arm64  • Android 13 (API 33)
    • sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64  • Android 14 (API 34) (emulator)
    • macOS (desktop)             • macos         • darwin-arm64   • macOS 14.3.1 23D60 darwin-arm64
    • Chrome (web)                • chrome        • web-javascript • Google Chrome 122.0.6261.129
    ! Error: Browsing on the local area network for iPhone HOME. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

Activity

  1. changed the title [-]Predictive back can close app when navigationstack is not empty[/-] [+]Predictive back can close app when navigation stack is not empty[/+] on Mar 17, 2024
  2. darshankawar commented on Mar 18, 2024

    @darshankawar
    Member

    @Geara0
    Can you take a look at https://docs.flutter.dev/release/breaking-changes/android-predictive-back and see if it helps in your case or not ?

  3. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Mar 18, 2024
  4. Geara0 commented on Mar 18, 2024

    @Geara0
    Author

    @darshankawar
    No, migration guide can't help me in this case, because key functions in this example is
    showModalBottomSheet(context, useRootNavigator: true)
    Navigator.of(navKey.currentContext!).pop();
    and
    didPopRoute from RootBackButtonDispatcher
    which are functions that came from the default flutter package

    1. on app launch we have stack (at root): [NestedRoute]
    2. after going to second page: [NestedRoute]
    3. after opening modal via rootnavigator: [NestedRoute, Modal]
    4. after pop from rootcontext: [NestedRoute]

    then, GoRouter should handle pop in NestedRoute, but app closes even before GoRouter registers any event

    Why I think it's not GoRouter issue?

    1. I operate only with android back button, which is handled by RootBackButtonDispatcher's, which calls the same methods independent of any flag in androidManifest
    2. Behaviour is based only on enableOnBackInvokedCallback flag
    3. GoRouter backButtonDispatcher is just RootBackButtonDispatcher
      https://github.com/flutter/packages/blob/a757073ac4eaf05b7516d3d0488e5c98b221043f/packages/go_router/lib/src/router.dart#L186

    I put debugPoints everywhere across flutter and go_router source files and saw that flutter calls function (can't find it rn, maybe will come back to that later) that does following

    case ios:
    case macos:
    case web:
    case windows:
    case fuchsia:
        return some_bool
    case android:
       return call_to_method_channel
    

    Why even use GoRouter then?

    Idk how to create nested routes with flutter only, but I'm sure that issue will occur even if I rebuild app with flutter default navigation (if possible)

  5. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Mar 18, 2024
  6. darshankawar commented on Mar 19, 2024

    @darshankawar
    Member

    Thanks for the update. Check below issues and see if they resemble your case or not:

    #140869
    #139398

  7. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Mar 19, 2024
  8. Geara0 commented on Mar 19, 2024

    @Geara0
    Author

    @darshankawar
    #140869 is not related bc android:enableOnBackInvokedCallback doesn't affect this issue
    (same result when true/false)
    #139398 is not realted bc my issue isn't go_router issue
    (And go router works fine in my case.)

    As I said earlier

    app closes even before GoRouter registers any event


    My issue is about android:enableOnBackInvokedCallback value affecting how nested routing works
    if false - all works fine
    if true - nested navigation doesn't receive pop callback, instead app closes
    and this close app behaviour is handled by flutter itself

  9. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Mar 19, 2024
  10. darshankawar commented on Mar 20, 2024

    @darshankawar
    Member

    Thanks for the update. I was able to replicate the reported behavior using details provided.

    stable, master flutter doctor -v
    [!] Flutter (Channel stable, 3.19.3, on macOS 12.2.1 21D62 darwin-x64, locale
        en-GB)
        • Flutter version 3.19.3 on channel stable at
          /Users/dhs/documents/fluttersdk/flutter
        ! Warning: `flutter` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
          your current Flutter SDK checkout at
          /Users/dhs/documents/fluttersdk/flutter. Consider adding
          /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
        ! Warning: `dart` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
          current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
          Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
          of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision ba39319843 (5 days ago), 2024-03-07 15:22:21 -0600
        • Engine revision 2e4ba9c6fb
        • Dart version 3.3.1
        • DevTools version 2.31.1
        • If those were intentional, you can disregard the above warnings; however
          it is recommended to use "git" directly to perform update checks and
          upgrades.
    
    [!] Xcode - develop for iOS and macOS (Xcode 12.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        ! Flutter recommends a minimum Xcode version of 13.
          Download the latest version or update via the Mac App Store.
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (5 available)
        • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
        • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
          ios            • iOS 14.4.1 18D61
        • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
          ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
        • macOS (desktop)            • macos                                    •
          darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)               • chrome                                   •
          web-javascript • Google Chrome 98.0.4758.80
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    ! Doctor found issues in 1 category.
    
    [!] Flutter (Channel master, 3.21.0-11.0.pre.43, on macOS 12.2.1 21D62
        darwin-x64, locale en-GB)
        • Flutter version 3.21.0-11.0.pre.43 on channel master at
          /Users/dhs/documents/fluttersdk/flutter
        ! Warning: `flutter` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
          your current Flutter SDK checkout at
          /Users/dhs/documents/fluttersdk/flutter. Consider adding
          /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
        ! Warning: `dart` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
          current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
          Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
          of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 056f25c220 (59 minutes ago), 2024-03-20 00:31:11 -0400
        • Engine revision 24b84203f6
        • Dart version 3.4.0 (build 3.4.0-250.0.dev)
        • DevTools version 2.34.0-dev.5
        • If those were intentional, you can disregard the above warnings; however
          it is recommended to use "git" directly to perform update checks and
          upgrades.
    
    [!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
        • Android SDK at /Users/dhs/Library/Android/sdk
        ✗ cmdline-tools component is missing
          Run `path/to/sdkmanager --install "cmdline-tools;latest"`
          See https://developer.android.com/studio/command-line for more details.
        ✗ Android license status unknown.
          Run `flutter doctor --android-licenses` to accept the SDK licenses.
          See https://flutter.dev/docs/get-started/install/macos#android-setup for
          more details.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 13C100
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] IntelliJ IDEA Ultimate Edition (version 2021.3.2)
        • IntelliJ at /Applications/IntelliJ IDEA.app
        • Flutter plugin version 65.1.4
        • Dart plugin version 213.7228
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.29.0
    
    [✓] Connected device (3 available)
        • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios
          • iOS 15.3.1 19D52
        • macOS (desktop)           • macos                                    •
          darwin-x64     • macOS 12.2.1 21D62 darwin-x64
        • Chrome (web)              • chrome                                   •
          web-javascript • Google Chrome 109.0.5414.119
    
    [✓] Network resources
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category.
          
    [!] Xcode - develop for iOS and macOS (Xcode 12.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        ! Flutter recommends a minimum Xcode version of 13.
          Download the latest version or update via the Mac App Store.
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (5 available)
        • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
        • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
          ios            • iOS 14.4.1 18D61
        • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
          ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
        • macOS (desktop)            • macos                                    •
          darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)               • chrome                                   •
          web-javascript • Google Chrome 98.0.4758.80
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    ! Doctor found issues in 1 category.
    
    
    
    
  11. 35 remaining items

  12. added
    ⏳Bot is counting down the days until it unassigns the issue
    on Jul 23, 2025
  13. flutter-triage-bot commented on Oct 9, 2025

    @flutter-triage-bot

    This issue was assigned to @justinmc but has had no status updates in a long time. To remove any ambiguity about whether the issue is being worked on, the assignee was removed.

  14. removed
    ⏳Bot is counting down the days until it unassigns the issue
    on Oct 9, 2025
  15. added
    P2Important issues not at the top of the work list
    and removed
    P1High-priority issues at the top of the work list
    on Oct 14, 2025
  16. added and removed
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Aug 26, 2026
  17. Henry-Hiles commented on Oct 5, 2026

    @Henry-Hiles

    Can confirm this still happens for me on Flutter 3.47.4, where a drawer is in the stack but the entire app closes on predictive back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listf: routesNavigator, Router, and related APIs.found in release: 3.19Found to occur in 3.19found in release: 3.21Found to occur in 3.21has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: go_routerThe go_router packageplatform-androidAndroid applications specificallyteam-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem teamworkaround availableThere is a workaround available to overcome the issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions