Skip to content

Add to app engine does not report correct life cycle state during warm up #39832

Closed

Description

@chunhtai

Steps to Reproduce

  1. follow this document to setup add to app https://github.com/flutter/samples/tree/master/experimental/add_to_app/SimpleIOSExample
  2. update the main function in this file
    experimental/add_to_app/example_module/lib/main.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 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Module Title',
      routes: {
        '/': (context) => FullScreenView(),
        '/mini': (context) => MiniView(),
      },
    );
  }
}

class FullScreenView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('A Full-screen Flutter View'),
      ),
      body: const MiniView(showExit: true),
    );
  }
}

class MiniView extends StatefulWidget {
  final bool showExit;

  const MiniView({this.showExit = false});

  @override
  _MiniViewState createState() => _MiniViewState();
}

class _MiniViewState extends State<MiniView> with WidgetsBindingObserver {
  int count = 0;

  AppLifecycleState _lastLifecycleState;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    setState(() {
      _lastLifecycleState = state;
    });
  }

  @override
  Widget build(BuildContext context) {
    final mediaInfo = MediaQuery.of(context);
    print('current state $_lastLifecycleState');
    return SizedBox.expand(
      child: Stack(
        children: [
          Positioned.fill(
            child: DecoratedBox(
              decoration: BoxDecoration(
                color: Theme.of(context).scaffoldBackgroundColor,
              ),
            ),
          ),
          Positioned.fill(
            child: Opacity(
              opacity: .25,
              child: FittedBox(
                fit: BoxFit.cover,
                child: FlutterLogo(),
              ),
            ),
          ),
          Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  'Window is ${mediaInfo.size.width} x '
                  '${mediaInfo.size.height}.',
                  style: Theme.of(context).textTheme.headline,
                ),
                SizedBox(height: 16),
                Text(
                  'Taps: $count',
                  style: Theme.of(context).textTheme.headline,
                ),
                SizedBox(height: 16),
                RaisedButton(
                  onPressed: () => setState(() => count++),
                  child: Text('Tap me!'),
                ),
                if (widget.showExit) ...[
                  SizedBox(height: 16),
                  RaisedButton(
                    onPressed: () => SystemNavigator.pop(),
                    child: Text('Exit this screen'),
                  ),
                ],
              ],
            ),
          ),
        ],
      ),
    );
  }
}
  1. run the app, and the widget is first build during initialization with lifecyclestate = null which it should be pause.

Activity

  1. added
    engineflutter/engine related. See also e: labels.
    a: existing-appsIntegration with existing apps via the add-to-app flow
    on Sep 4, 2019
  2. self-assigned this
    on Sep 4, 2019
  3. chunhtai commented on Sep 4, 2019

    @chunhtai
    ContributorAuthor
  4. xster commented on Sep 4, 2019

    @xster
    Member

    I assume you specifically meant that _lastLifecycleState is null because WidgetsBindingObserver.didChangeAppLifecycleState is never called right?

  5. chunhtai commented on Sep 4, 2019

    @chunhtai
    ContributorAuthor

    It is never called, but there is more to it.

    When the binding first initialized, it will poll the state from window.initialLifecycleState which engine should attach its state to. The engine, however, doesn't seems to attach anything to it during the warm up. Otherwise, WidgetsBindingObserver.didChangeAppLifecycleState would have been called when widget is built

  6. chunhtai commented on Nov 22, 2019

    @chunhtai
    ContributorAuthor

    Change reverted and is pending on #45270

  7. chunhtai commented on Jan 18, 2020

    @chunhtai
    ContributorAuthor

    another internal test failed

  8. github-actions commented on Aug 23, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  9. locked as resolved and limited conversation to collaborators on Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

a: existing-appsIntegration with existing apps via the add-to-app flowengineflutter/engine related. See also e: labels.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions