Repository: flutter
Issue: [go router] Go router doesn't trigger pop callback function after app is restored and second route is popped
Labels: package,has reproducible steps,P2,p: go_router,team-go_router,triaged-go_router,found in release: 3.13,found in release: 3.14

### Is there an existing issue for this?

- [X] I have searched the [existing issues](https://github.com/flutter/flutter/issues)
- [X] I have read the [guide to filing a bug](https://flutter.dev/docs/resources/bug-reports)

### Steps to reproduce

I have 2 routes, for example, route /A and route /A/B. Route B pops an object that a callback function in route A saves in the cloud.

**The bug **

After turning on Don't keep activities in android developer mode (to test state restoration), When the app goes from route /A to route /A/B and then the app is minimised, on restoring the app when the route /A/B pops the value, the route /A does not trigger the callback function to save the object.

Steps to reproduce:-

1) Turn on Don't keep activities in developer mode android.
2) Flutter create bug
3) install go_router in the project.
4) Copy contents of main.dart from the code sample into your project main.dart
5) Run the App
6) Click on + icon a few times to increase the counter on the screen
7) Click on "Go to Second Screen" button
8) Minimise the app. Put it in background state
9) Bring the app back to foreground.
10) Click on "Pop with Data" button on the second screen
11) Note that debug console doesn't show "POPPED WITH VALUE TEST" statement in the logs


### Expected results

The expected result on Step 11 of steps to reproduce is to see "POPPED WITH VALUE TEST" in debug console log statements.

### Actual results

The callback function the print "POPPED WITH VALUE TEST" in HomeScreen doesn't trigger after the app is restored from a background state.

### Code sample

<details><summary>Code sample</summary>

```dart
import 'dart:developer';

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) {
    return MaterialApp.router(
      routerConfig: RouterSingleton.getInstance().router,
      title: 'Flutter Demo',
      restorationScopeId: "app",
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with RestorationMixin {
  RestorableInt _counter = RestorableInt(0);

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    _counter.dispose();
    super.dispose();
  }

  @override
  String? get restorationId => "homescreen";

  @override
  void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
    // TODO: implement restoreState
    registerForRestoration(_counter, "counter");
  }

  void _incrementCounter() {
    setState(() {
      _counter.value++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '${_counter.value}',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            ElevatedButton(
              onPressed: () {
                // Beamer.of(context).beamToNamed('seco');
                GoRouter.of(context).pushNamed("second").then((value) {
                  log("POPPED WITH VALUE $value");
                });
              },
              child: Text("Go to Second screen"),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

class RouterSingleton {
  // Static instance of the class
  static RouterSingleton? _singleton;

  // Your GoRouter instance
  late final GoRouter _router;

  // Named constructor
  RouterSingleton._internal() {
    _router = GoRouter(
      initialLocation: '/',
      restorationScopeId: "app-router",
      routes: <GoRoute>[
        GoRoute(
          path: '/',
          name: "home",
          pageBuilder: (context, state) => const MaterialPage(
            restorationId: 'homepageid',
            child: MyHomePage(title: "Home"),
          ),
        ),
        GoRoute(
          path: '/second',
          name: "second",
          pageBuilder: (context, state) => const MaterialPage(
            restorationId: 'second-route',
            child: SecondScreen(),
          ),
        ),
      ],
    );
  }

  // Static method to access the Singleton instance
  static RouterSingleton getInstance() {
    _singleton ??= RouterSingleton._internal();
    return _singleton!;
  }

  // Getter to retrieve the GoRouter instance
  GoRouter get router => _router;
}

class SecondScreen extends StatefulWidget {
  const SecondScreen({super.key});

  @override
  State<SecondScreen> createState() => _SecondScreenState();
}

class _SecondScreenState extends State<SecondScreen> with RestorationMixin {
  @override
  // TODO: implement restorationId
  String? get restorationId => "second_screen";

  @override
  void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
    // TODO: implement restoreState
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Screen"),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ElevatedButton(
            onPressed: () {
              GoRouter.of(context).pop("TEST");
            },
            child: Text("Pop with data"),
          )
        ],
      ),
    );
  }
}

```

</details>


### Screenshots or Video

<details>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

https://github.com/flutter/flutter/assets/17780921/7a73e22b-2ec1-4368-976e-3ac780e2f097


</details>


### Logs

<details><summary>Logs</summary>

```console
Launching lib/main.dart on Android SDK built for arm64 in debug mode...
✓  Built build/app/outputs/flutter-apk/app-debug.apk.
Connecting to VM Service at ws://127.0.0.1:58786/iNazP4MtnhU=/ws
D/eglCodecCommon( 6615): setVertexArrayObject: set vao to 0 (0) 1 0
D/EGL_emulation( 6615): eglMakeCurrent: 0x7a31e6db00: ver 3 0 (tinfo 0x7a31fda9e0)
D/eglCodecCommon( 6615): setVertexArrayObject: set vao to 0 (0) 1 2
[log] POPPED WITH VALUE TEST
D/EGL_emulation( 6615): eglMakeCurrent: 0x7a31e6db00: ver 3 0 (tinfo 0x7a31fda9e0)
D/EGL_emulation( 6615): eglMakeCurrent: 0x7a30429b60: ver 3 0 (tinfo 0x7a2ea3e700)
W/ActivityThread( 6615): handleWindowVisibility: no activity for token android.os.BinderProxy@d05aa13
D/eglCodecCommon( 6615): setVertexArrayObject: set vao to 0 (0) 0 0
D/EGL_emulation( 6615): eglCreateContext: 0x7a30429c00: maj 3 min 0 rcv 3
D/eglCodecCommon( 6615): setVertexArrayObject: set vao to 0 (0) 0 0
D/EGL_emulation( 6615): eglCreateContext: 0x7a31e6aa40: maj 3 min 0 rcv 3
D/HostConnection( 6615): HostConnection::get() New Host Connection established 0x7a31e6a9a0, tid 6671
D/HostConnection( 6615): HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_sync_buffer_data GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_3_0 
D/EGL_emulation( 6615): eglMakeCurrent: 0x7a31e6aa40: ver 3 0 (tinfo 0x7a31eb30a0)

```

</details>


### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.13.1, on macOS 13.4.1 22F82 darwin-arm64, locale en-AU)
    • Flutter version 3.13.1 on channel stable at /Users/adi/Desktop/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision e1e47221e8 (13 days ago), 2023-08-22 21:43:18 -0700
    • Engine revision b20183e040
    • Dart version 3.1.0
    • DevTools version 2.25.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc1)
    • Android SDK at /Users/adi/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0-rc1
    • ANDROID_HOME = /Users/adi/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E300c
    • CocoaPods version 1.12.0

[✓] Android Studio (version 2022.1)
    • Android Studio at /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 11.0.15+0-b2043.56-8887301)

[✓] VS Code (version 1.81.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.70.0

[✓] Connected device (1 available)
    • Android SDK built for arm64 (mobile) • emulator-5554 • android-arm64 • Android 9 (API 28)
      (emulator)

[✓] Network resources
    • All expected network resources are available.

• No issues found!
```

</details>