Repository: flutter
Issue: [Android] Deferred Components loads only for the first time. 
Labels: platform-android,engine,a: production,P2,team-android,triaged-android

I am using the deferred components in one of my application. The configuration and settings for making up the deferred components went successfully. I followed the [Flutter Documentation](https://docs.flutter.dev/perf/deferred-components) to implement it. 

When I build app-release bundle and install it into my device for testing purpose the deferred component loads successfully as the doc provided. 

But when I upload the application on playstore,and once app is installed and begin to use, it installs and load the deferred component successfully. but when I kill the app from background and load it again the deferred loading placeholder is keep coming although it runs successfully on the first-go.

**Using the Flutter Stable 2.8.1 version.**

<details>
<summary>sample code</summary>

*Deferred_Widget.dart*

```dart
// Copyright 2019 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'package:flutter/material.dart';

typedef LibraryLoader = Future<void> Function();
typedef DeferredWidgetBuilder = Widget Function();

class DeferredWidget extends StatefulWidget {
  DeferredWidget(
    this.libraryLoader,
    this.createWidget, {
    Key? key,
    Widget? placeholder,
  })  : placeholder = placeholder ?? Container(),
        super(key: key);

  final LibraryLoader libraryLoader;
  final DeferredWidgetBuilder createWidget;
  final Widget placeholder;
  static final Map<LibraryLoader, Future<void>> moduleLoaders = {};
  static final Set<LibraryLoader> loadedModules = {};

  @override
  _DeferredWidgetState createState() => _DeferredWidgetState();
}

class _DeferredWidgetState extends State<DeferredWidget> {
  _DeferredWidgetState();

  Widget? _loadedChild;
  DeferredWidgetBuilder? _loadedCreator;
  bool isError = false;
  String errorMsg = '';
  String helperMsg = '';
  @override
  void initState() {

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: widget.libraryLoader(),
      builder: (BuildContext context, AsyncSnapshot<void> snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          if (snapshot.hasError) {
            return ErrorWidget(errorMsg: snapshot.error.toString());
          }
          return widget.createWidget();
        }
        return const DeferredLoadingPlaceholder(
          name: 'wallet',
          msg: '',
          helperMsg: '',
        );
      },
    );
  }
}

class ErrorWidget extends StatelessWidget {
  const ErrorWidget({Key? key, required this.errorMsg}) : super(key: key);
  final String errorMsg;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        decoration: BoxDecoration(
            color: Colors.grey[700],
            border: Border.all(
              width: 20,
              color: (Colors.grey[700])!,
            ),
            borderRadius: const BorderRadius.all(Radius.circular(10))),
        width: 250,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            const Text(
              'Error Widget..',
              style: TextStyle(
                  color: Colors.white,
                  fontSize: 14,
                  fontWeight: FontWeight.bold),
            ),
            Container(height: 10),
            Text(
              errorMsg,
              style: const TextStyle(
                  color: Colors.white,
                  fontSize: 12,
                  fontWeight: FontWeight.bold),
            ),
            Container(height: 20),
          ],
        ),
      ),
    );
  }
}

class DeferredLoadingPlaceholder extends StatelessWidget {
  const DeferredLoadingPlaceholder(
      {Key? key,
      required this.name,
      required this.msg,
      required this.helperMsg})
      : super(key: key);

  final String name;
  final String msg;
  final String helperMsg;
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        decoration: BoxDecoration(
            color: Colors.grey[700],
            border: Border.all(
              width: 20,
              color: (Colors.grey[700])!,
            ),
            borderRadius: const BorderRadius.all(Radius.circular(10))),
        width: 250,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              '$name is installing.',
              style: const TextStyle(
                  color: Colors.white,
                  fontSize: 14,
                  fontWeight: FontWeight.bold),
            ),
            Container(height: 10),
            Text(
              '$name is a deferred component which are downloaded and installed at runtime.',
              style: const TextStyle(
                  color: Colors.white,
                  fontSize: 12,
                  fontWeight: FontWeight.bold),
            ),
            Container(height: 20),
            const Center(child: CircularProgressIndicator()),
            Container(height: 10),
            Text(
              '$msg :: $helperMsg ',
              style: const TextStyle(
                  color: Colors.white,
                  fontSize: 12,
                  fontWeight: FontWeight.bold),
            ),
          ],
        ),
      ),
    );
  }
}
```

### Set DeferredWidget inside application routes settings as:

```dart
import 'package:flutter/material.dart';
import 'package:flutter/semantics.dart';
import 'package:schannel_app/src/deferred_widget.dart';
import 'package:schannel_app/src/home/home_view.dart';
import 'package:schannel_app/src/routes/route_list.dart';
import 'package:schannel_app/src/wallet/wallet_view.dart' deferred as wallet;

class Routes {
  static Map<String, WidgetBuilder> getAll() => _routes;
  String helperMsg = '';

  static final Map<String, WidgetBuilder> _routes = {
    AppRouteList.home: (context) => const HomeView(),
    AppRouteList.walletApp: (context) => BackgroundWrapper(
          childWidget: DeferredWidget(
            wallet.loadLibrary,
            () => wallet.WalletView(), // ignore: prefer_const_constructors
            // placeholder: const DeferredLoadingPlaceholder(
            //   name: 'Wallet',
            // ),
          ),
        ),
  };
}

// background wrapper widget
class BackgroundWrapper extends StatefulWidget {
  const BackgroundWrapper({
    Key? key,
    required this.childWidget,
    this.alignment = AlignmentDirectional.bottomStart,
    this.hasBottomNavBar = false,
  }) : super(key: key);

  final Widget childWidget;
  final bool hasBottomNavBar;
  final AlignmentDirectional alignment;

  @override
  _BackgroundWrapperState createState() => _BackgroundWrapperState();
}

class _BackgroundWrapperState extends State<BackgroundWrapper> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Semantics(
            sortKey: const OrdinalSortKey(1),
            child: RestorationScope(
                restorationId: 'childWidget_wrapper',
                child: widget.childWidget),
          ),
        ],
      ),
    );
  }
}
```
</details>

### [More information can be traced from](https://github.com/flutter/gallery/issues/588)

### <img src="https://user-images.githubusercontent.com/49707515/150672348-c49c627b-0784-44b0-aef1-483d1c1d413b.gif" width ="300">