不调用AppLifeCycleState.detached [英] AppLifeCycleState.detached is not called

查看:304
本文介绍了不调用AppLifeCycleState.detached的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() {
    return _MyHomePageState();
  }
}

class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

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

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    print(state);
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        //do something here before pop
        return true;
      },
      child: Scaffold(
        body: Container(),
      ),
    );
  }
}

我的应用在上方.我试图通过概述按钮并通过滑动关闭应用程序来检测我的应用程序何时在Android上被杀死.问题是AppLifeCycleState.detached从未传递给我的回调.如果我通过按root上的后退"按钮关闭该应用程序,则会将其打印出来.如果我通过从概览按钮滑动来杀死它,则回调不存在.我本质上是在尝试获取本机android的onDestroy()调用.

My app is above. I am trying to detect when my app is killed on Android with overview button and by closing the app by swiping. The problem is AppLifeCycleState.detached is never passed to my callback. If I close the app by pressing back button on root, it is printed. If I kill it with swiping from overview button the callback is not there. I am essentially trying to get the onDestroy() call of native android.

这是我得到的日志:

D/SurfaceView(24406): windowStopped(true) false io.flutter.embedding.android.FlutterSurfaceView{d98238 V.E...... ........ 0,0-1080,2154} of ViewRootImpl@185dd4c[MainActivity]
I/flutter (24406): AppLifecycleState.paused
Lost connection to device.

ExpectedLog:

ExpectedLog:

D/SurfaceView(25394): windowStopped(true) false io.flutter.embedding.android.FlutterSurfaceView{2d5a56d V.E...... ........ 0,0-1080,2154} of ViewRootImpl@28f4397[MainActivity]
I/flutter (25394): AppLifecycleState.paused
I/flutter (25394): AppLifecycleState.detached
Lost connection to device.

推荐答案

显然,这是一个持续不断的bug. https://github.com/flutter/flutter/issues/57594

Apparently, this is an ongoing bug with flutter. https://github.com/flutter/flutter/issues/57594

这篇关于不调用AppLifeCycleState.detached的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆