另请参阅https://flutter.io/flutter-for-android/#how-do-i-listen-to-android-
activity-lifecycle-events
您可以收听不活动,已暂停和已分离的内容。这可能为时过早,但通常最好是过早且频繁地进行一些清理,而不是根本不做:
WidgetsBinding.instance.addObserver(LifecycleEventHandler( detachedCallBack: () async => widget.appController.persistState(), resumeCallBack: () async { _log.finest('resume...'); }));class LifecycleEventHandler extends WidgetsBindingObserver { LifecycleEventHandler({this.resumeCallBack, this.detachedCallBack}); final FutureVoidCallback resumeCallBack; final FutureVoidCallback detachedCallBack;// @override// Future<bool> didPopRoute()// @override// void didHaveMemoryPressure() @override Future<void> didChangeAppLifecycleState(AppLifecycleState state) async { switch (state) { case AppLifecycleState.inactive: case AppLifecycleState.paused: case AppLifecycleState.detached: await detachedCallBack(); break; case AppLifecycleState.resumed: await resumeCallBack(); break; } _log.finest('''============================================================= $state============================================================='''); }// @override// void didChangeLocale(Locale locale)// @override// void didChangeTextScaleFactor()// @override// void didChangeMetrics();// @override// Future<bool> didPushRoute(String route)}
编辑
根据2019年11月4日的拉取请求,该枚举
AppLifecycleState.suspending被重命名为
AppLifecycleState.detached。如果您使用的Flutter版本低于1.12,则仍必须使用
AppLifecycleState.suspending。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)