Repository: flutter
Issue: Hardware back button race condition with nested Router in Navigator 2.0
Labels: platform-android,framework,engine,f: routes,has reproducible steps,P2,found in release: 3.3,found in release: 3.7,team-android,triaged-android

## Steps to Reproduce

1. Execute `flutter run` on the code sample in emulator
2. Click "hardware" back button slowly
3. Click "hardware" back button quickly

**Expected results:**
Back button behaviour is the same

**Actual results:**
Back button behaviour is different

https://user-images.githubusercontent.com/5578017/213591719-4b5da94c-ae10-45eb-bb19-8ecd061a6275.mp4


<details>
<summary>Code sample</summary>
https://dartpad.dev/?id=bc4d1013a9b0fd084454b7e372d7c3d3

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(MainApp());
}

class MainApp extends StatelessWidget {
  final RouterDelegate<AppState> _routerDelegate = MainRouterDelegate();

  MainApp({super.key});
  @override
  Widget build(BuildContext context) =>
    MaterialApp.router(
      routerDelegate: _routerDelegate,
      backButtonDispatcher: RootBackButtonDispatcher(),
    );
}

class MainRouterDelegate extends RouterDelegate<AppState>
    with ChangeNotifier, PopNavigatorRouterDelegateMixin<AppState> {

  @override
  final GlobalKey<NavigatorState> navigatorKey;
  
  MainRouterDelegate() : navigatorKey = GlobalKey<NavigatorState>();

  final AppState appState = AppState();
  
  @override
  Widget build(BuildContext context) => Navigator(
      key: navigatorKey,
      pages: [
        MaterialPage(
          child: MainScreen(
            key: const ValueKey("mainScreen"),
            text: "My Page",
            appState: appState,
          )
        ),
        const MaterialPage(child: InnerScreen(text: "Inner screen")) 
      ],
      onPopPage: (route, result) {
        print('MainRouter#onPopPage');

        if (!route.didPop(result)) {
          return false;
        }

        return true;
      },
    );

  @override
  Future<void> setNewRoutePath(AppState configuration) async {
  }
}

class MainScreen extends StatefulWidget {
  MainScreen({super.key, required this.text, required this.appState}): _innerRouterDelegate = InnerRouterDelegate();

  final String text;
  final AppState appState;
  final RouterDelegate<AppState> _innerRouterDelegate;

  @override
  State<MainScreen> createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {

  @override
  Widget build(BuildContext context) => Scaffold(
      body: Router(
        routerDelegate: widget._innerRouterDelegate,
      ),
    );
}

class InnerRouterDelegate extends RouterDelegate<AppState>
    with ChangeNotifier, PopNavigatorRouterDelegateMixin<AppState> {

  @override
  final GlobalKey<NavigatorState> navigatorKey;

  InnerRouterDelegate() : navigatorKey = GlobalKey<NavigatorState>(); 
  
  @override
  Widget build(BuildContext context) => Navigator(
      key: navigatorKey,
      pages: const [
        MaterialPage(
          child: InnerScreen(
            key: ValueKey("innerScreen"),
            text: "Main screen",
          )
        ),
      ],
      onPopPage: (route, result) {
        print('InnerRouter#onPopPage');

        if (!route.didPop(result)) {
          return false;
        }

        return true;
      },
    );

  @override
  Future<void> setNewRoutePath(AppState configuration) async {
  }
}

class AppState extends ChangeNotifier {
}

class InnerScreen extends StatelessWidget {
  const InnerScreen({super.key, required this.text});

  final String text;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: Column(
          children: [
            Text(text),
          ],
        ),
      ),
    );
  }
}
```

</details>

<details>
  <summary>Logs</summary>

```
[+7527 ms] I/flutter (13036): MainRouter#onPopPage
[+1142 ms] I/flutter (13036): MainRouter#onPopPage
[+1164 ms] I/flutter (13036): MainRouter#onPopPage
```


```
   info - Avoid `print` calls in production code - lib\main.dart:43:9 - avoid_print
   info - Avoid `print` calls in production code - lib\main.dart:99:9 - avoid_print
```


```
[√] Flutter (Channel stable, 3.3.4, on Microsoft Windows [Version 10.0.19044.2006], locale pl-PL)
    • Flutter version 3.3.4 on channel stable at C:\Users\User\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision eb6d86ee27 (3 months ago), 2022-10-04 22:31:45 -0700
    • Engine revision c08d7d5efc
    • Dart version 2.18.2
    • DevTools version 2.15.0

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at C:\Users\User\AppData\Local\Android\sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[X] Visual Studio - develop for Windows
    X Visual Studio not installed; this is necessary for Windows development.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components

[√] Android Studio (version 2021.3)
    • Android Studio at C:\Program Files\Android\Android Studio
    • 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 11.0.13+0-b1751.21-8125866)

[√] VS Code (version 1.74.2)
    • VS Code at C:\Users\User\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.56.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19044.2006]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 107.0.5304.123
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 107.0.1418.42

[√] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.
```

</details>


<!--
      Consider also attaching screenshots and/or videos to better
      illustrate the issue.

      You can upload them directly on GitHub.
      Beware that video file size is limited to 10MB.
-->