Repository: flutter
Issue: [Android] Deeplink gets ignored when killed activity gets restored
Labels: platform-android,framework,d: api docs,f: routes,has reproducible steps,P2,found in release: 2.8,found in release: 2.9,team-android,triaged-android

Hello Flutter Team!
Thanks for the great work. I found a bug when working with deep links in one of my projects.
The problem is reproduced able on my simulator (Pixel 2 Api 30) and on my Xiaomi Mi2 Lite Device(Android 10).

My example code simply displays the parsed route of the intent in the center of the screen.
The behavior works as expected, when the app is freshly started by a deep link or when the flutter activity stays alive on an app switch and is brought back by a deep link.
Only when the activity gets restored from the background by some deepLink the link is ignored by the restarted app.

## Steps to Reproduce

<!-- Please include full steps to reproduce so that we can reproduce the problem. -->

1.  activate `[x] Dont keep activities` in the android developer settings on testing device
2.  run `flutter create bug`
3.  copy the attached `AndroidManifest.xml` to  to the `android/app/src/main` folder
4.  run `main.dart` in profile or release mode
5. switch to another app 
6. run `adb shell am start -a android.intent.action.VIEW \
    -c android.intent.category.BROWSABLE \
    -d "bug.deep.link:/foo"`

**Expected results:** 

* The app comes back to the foreground
* The Text in the View states `/foo`.

**Actual results:**

* The app comes back to the foreground
* The Text in the View states `/`.

<details>
<summary>AndroidManifest.xml</summary>

```xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bug">
   <application
        android:label="bug"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <!-- Deep linking -->
            <meta-data android:name="flutter_deeplinking_enabled" android:value="true" />
            <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="bug.deep.link" />
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>
```

</details>

<details>
<summary>main.dart</summary>

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final AppRouterInformationParser _routerInformationParser =
      AppRouterInformationParser();
  final AppRouterDelegate _routerDelegate = AppRouterDelegate();

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      routerDelegate: _routerDelegate,
      routeInformationParser: _routerInformationParser,
    );
  }
}

class ParsedRoute {
  final String? route;

  ParsedRoute(this.route);
}

class AppRouterInformationParser extends RouteInformationParser<ParsedRoute> {
  @override
  Future<ParsedRoute> parseRouteInformation(
      RouteInformation routeInformation) async {
    return ParsedRoute(routeInformation.location);
  }
}

class AppRouterDelegate extends RouterDelegate<ParsedRoute>
    with ChangeNotifier, PopNavigatorRouterDelegateMixin<ParsedRoute> {
  ParsedRoute? p;
  final GlobalKey<NavigatorState>? _navigatorKey = GlobalKey();
  @override
  Widget build(BuildContext context) => Navigator(
        onPopPage: (route, result) => route.didPop(result),
        pages: [
          MaterialPage(child: DisplayRouteView(route: p)),
        ],
        key: _navigatorKey,
      );

  @override
  GlobalKey<NavigatorState>? get navigatorKey => _navigatorKey;

  @override
  Future<void> setNewRoutePath(ParsedRoute configuration) async {
    p = configuration;
  }
}

class DisplayRouteView extends StatelessWidget {
  final ParsedRoute? route;
  const DisplayRouteView({Key? key, this.route}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text(
          route?.route ?? 'No route parsed',
          style: const TextStyle(fontSize: 23),
        ),
      ),
    );
  }
}

```

</details>

<details>
  <summary>Flutter Doctor</summary>

```
[✓] Flutter (Channel stable, 2.8.1, on macOS 11.6 20G165 darwin-arm, locale en-DE)
    • Flutter version 2.8.1 at /Users/jakobkrause/fvm/versions/2.8.1
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 77d935af4d (5 weeks ago), 2021-12-16 08:37:33 -0800
    • Engine revision 890a5fca2e
    • Dart version 2.15.1

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/jakobkrause/Library/Android/sdk
    • Platform android-31, build-tools 31.0.0
    • ANDROID_HOME = /Users/jakobkrause/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.0)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
    • 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.10+0-b96-7249189)
```

</details>