如何在Flutter Android插件包中处理Android生命周期 [英] How to handle android lifecycle in flutter android plugin package
问题描述
我需要知道android插件包中flutter应用程序视图的当前状态.
I need to know the current state of the flutter app view in an android plugin package.
现在,我使用 https://docs.flutter.io/flutter/widgets/WidgetsBindingObserver-class.html 并将其传递给我的插件.
For now, I observe the state in the flutter view with https://docs.flutter.io/flutter/widgets/WidgetsBindingObserver-class.html and pass it then to my plugin.
由于有时看起来并不完美(第一个事件未在Android上传递),我想直接从android插件获取状态.
As this seems sometimes not perfect (the first event is not passed on Android) I would like to get the state directly from the android plugin.
在插件中,我可以获得注册商,并且可以注册其活动,但是如何观察其状态?
In the plugin I get the registrar and can its activity, but how do I observe the state of it?
推荐答案
Flutter不提供这些生命周期事件的挂钩.
Flutter doesn't provide hooks for these lifecycle events.
Android Jetpack(以前称为Architecture Components)添加了一种从活动接收生命周期事件的好方法:
Android Jetpack (formerly Architecture Components) added a nice way to receive lifecycle events from an activity:
缺点:仅当活动是AppCompatActivity
时,它才起作用. Flutter应用程序通常基于简单的Activity
,因此您必须告诉用户使用AppCompatActivity
.
Downside: It only works when the activity is an AppCompatActivity
. Flutter apps are usually based on a simple Activity
, so you have to tell your users to use an AppCompatActivity
instead.
这篇关于如何在Flutter Android插件包中处理Android生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!