简体   繁体   English

didChangeAppLifecycleState 不工作 Flutter

[英]didChangeAppLifecycleState is not working Flutter

I have the app of more than 20 screens, I want to perform a task if user is inactive/idle for a certain time.我有超过 20 个屏幕的应用程序,如果用户在一段时间内不活动/空闲,我想执行一项任务。 I am trying to get the app's lifecycle at root of app.我正在尝试在应用程序的根目录中获取应用程序的生命周期。 But i am not getting print statements on my logcat.但我没有在我的 logcat 上收到打印语句。 What wrong I have done here?我在这里做错了什么?

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
 
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
 

  @override
  State<StatefulWidget> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with WidgetsBindingObserver{


  @override
  didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        print('app resumed');
        break;
      case AppLifecycleState.inactive:
        print('app inactive');
        break;
      case AppLifecycleState.paused:
        print('app paused');
        break;
      case AppLifecycleState.detached:
        print('app detached');
        break;
    }
  }
   @override
  void initState() {
    WidgetsBinding.instance.addObserver(this);
    super.initState();
    _initializeTimer();
  }
  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
  }

I think it might be because your app is not changing its lifecycle state, here's what is said for the AppLifecycleState.inactive and AppLifecycleState.paused in the documentation:我认为这可能是因为您的应用程序没有改变其生命周期 state,以下是文档中对AppLifecycleState.inactiveAppLifecycleState.paused的说法:

inactive The application is in an inactive state and is not receiving user input.非活动应用程序处于非活动状态 state 并且未接收用户输入。

On iOS, this state corresponds to an app or the Flutter host view running in the foreground inactive state.在 iOS 上,此 state 对应于在前台运行的应用程序或 Flutter 主机视图处于非活动状态 Z9ED39E2EA93158EF5736A。 Apps transition to this state when in a phone call, responding to a TouchID request, when entering the app switcher or the control center, or when the UIViewController hosting the Flutter app is transitioning.应用程序在通话、响应 TouchID 请求、进入应用程序切换器或控制中心时,或者当托管 Flutter 应用程序的 UIViewController 正在转换时,应用程序将转换到此 state。

On Android, this corresponds to an app or the Flutter host view running in the foreground inactive state.在 Android 上,这对应于在前台非活动 state 中运行的应用程序或 Flutter 主机视图。 Apps transition to this state when another activity is focused, such as a split-screen app, a phone call, a picture-in-picture app, a system dialog, or another window.当关注另一个活动时,应用程序将转换到此 state,例如分屏应用程序、电话、画中画应用程序、系统对话框或另一个 window。

Apps in this state should assume that they may be paused at any time.此 state 中的应用程序应假定它们可以随时暂停。

paused The application is not currently visible to the user, not responding to user input, and running in the background. paused应用程序当前对用户不可见,不响应用户输入,并在后台运行。 When the application is in this state, the engine will not call the Window.onBeginFrame and Window.onDrawFrame callbacks.当应用程序在这个 state 中时,引擎不会调用 Window.onBeginFrame 和 Window.onDrawFrame 回调。 Android apps in this state should assume that they may enter the suspending state at any time.此 state 中的 Android 应用程序应假定它们可能随时进入暂停的 state。

source 资源

So basically none of these state will became active if your user is only waiting idly.因此,如果您的用户只是空闲等待,那么这些 state 基本上都不会激活。 The workaround that comes to my mind would be to use a RestartableTimer which would trigger the action you want after a certain "inactive" period of time and depending on how you've made your screen you could wrap it inside a GestureDetector to catch your user's input and reset the timer.我想到的解决方法是使用RestartableTimer ,它会在某个“非活动”时间段后触发您想要的操作,并且根据您制作屏幕的方式,您可以将其包装在GestureDetector中以捕捉用户的输入和复位定时器。

import 'package:flutter/material.dart';
import 'package:async/async.dart';

class HomePage extends StatefulWidget {
  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late final _timer =
      RestartableTimer(const Duration(seconds: 2), _timerAction);

  void _timerAction() {
    print("User is idle");
  }

  void _resetTimer() {
    print("Caught event, reset timer");
    _timer.reset();
  }

  @override
  void initState() {
    super.initState();
    _timer.reset(); // Start the timer
  }

  @override
  void dispose() {
    _timer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: _resetTimer,
      child: Scaffold(),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 flutter didChangeAppLifecycleState 从不运行 - flutter didChangeAppLifecycleState never runs 使用 flutter HookWidget 和 didChangeAppLifecycleState - Using flutter HookWidget and didChangeAppLifecycleState Flutter WidgetsBindingObserver 的 didChangeAppLifecycleState 未在 Windows 桌面上调用 - Flutter WidgetsBindingObserver's didChangeAppLifecycleState not being called on Windows Desktop 如何使用 flutter HookWidget 在 didChangeAppLifecycleState 生命周期挂钩中访问上下文? - How to have access Context in didChangeAppLifecycleState lifecycle hook using flutter HookWidget? 如何在didChangeAppLifecycleState(iOS和Android设备)上完全重启Flutter应用程序 - How to completely restart a flutter application on didChangeAppLifecycleState (iOS & android devices) 将 didChangeAppLifecycleState 应用于整个应用程序 - apply didChangeAppLifecycleState to the whole app didChangeAppLifecycleState 没有按预期工作 - didChangeAppLifecycleState doesn't work as expected 对于具有多路由的应用程序,我是否只需要 Main 中的一个 didChangeAppLifecycleState - do I need just one didChangeAppLifecycleState in Main for app with multi routes Flutter DateFormatter无法正常工作 - Flutter dateformatter not working correctly Flutter:setState()无法正常工作 - Flutter : setState() is not working properly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM