当前位置:网站首页>[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
2022-07-02 22:14:00 【Programmer community】
List of articles
- One 、Flutter Application life cycle
- Two 、 monitor Flutter Application life cycle
- 3、 ... and 、 Complete code example
- Four 、 Related resources
One 、Flutter Application life cycle
Application life cycle :
- resumed : Application enters the front desk ;
- paused : The app goes into the background ;
- inactive : App goes inactive ;
- detached : Applications are running but separate from components ;
Flutter Apply lifecycle state enumeration : There are four lifecycle states in this enumeration , The usage of each state is detailed in the source code comments ;
/// States that an application can be in.////// The values below describe notifications from the operating system./// Applications should not expect to always receive all possible/// notifications. For example, if the users pulls out the battery from the/// device, no notification will be sent before the application is suddenly/// terminated, along with the rest of the operating system.////// See also:////// * [WidgetsBindingObserver], for a mechanism to observe the lifecycle state/// from the widgets layer.enum AppLifecycleState {
/// The application is visible and responding to user input. resumed, /// The application is in an inactive state and is not receiving user input. /// /// On iOS, this state corresponds to an app or the Flutter host view running /// in the foreground inactive state. 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. /// /// On Android, this corresponds to an app or the Flutter host view running /// in the foreground inactive state. 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. /// /// Apps in this state should assume that they may be [paused] at any time. inactive, /// The application is not currently visible to the user, not responding to /// user input, and running in the background. /// /// When the application is in this state, the engine will not call the /// [Window.onBeginFrame] and [Window.onDrawFrame] callbacks. paused, /// The application is still hosted on a flutter engine but is detached from /// any host views. /// /// When the application is in this state, the engine is running without /// a view. It can either be in the progress of attaching a view when engine /// was first initializes, or after the view being destroyed due to a Navigator /// pop. detached,}
Two 、 monitor Flutter Application life cycle
monitor Flutter Application life cycle , Need on page StatefulWidget Of initState Register in the page lifecycle function WidgetsBindingObserver The observer ;
@override void initState() {
super.initState(); /// If you want to monitor the application lifecycle , First bind the observer , /// When the binding is complete , If the application lifecycle changes , /// It's going to come back didChangeAppLifecycleState Method ; WidgetsBinding.instance.addObserver(this); }
registered WidgetsBindingObserver After the observer , When applying lifecycle changes , It's going to come back WidgetsBindingObserver Class didChangeAppLifecycleState Method , Its AppLifecycleState state The parameter is the current application lifecycle state ;
/// When the application lifecycle changes , The method is called back @override void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state); print(" Current application lifecycle state : ${state}"); if(state == AppLifecycleState.paused){
print(" The app goes into the background paused"); }else if(state == AppLifecycleState.resumed){
print(" Application enters the front desk resumed"); }else if(state == AppLifecycleState.inactive){
// App goes inactive , Tathagata made a phone call , The phone application enters the front desk // This application enters this state print(" App goes inactive inactive"); }else if(state == AppLifecycleState.detached){
// Applications are still Flutter Running on the engine , But with the host View Component separation print(" The application enters detached state detached"); } }
stay StatefulWidget The page needs to be removed when it is destroyed WidgetsBindingObserver The observer ;
/// Remove the observer registered in the component @override void dispose() {
super.dispose(); WidgetsBinding.instance.removeObserver(this); }
3、 ... and 、 Complete code example
Complete code example :
import 'package:flutter/material.dart';class AppLifeCyclePage extends StatefulWidget {
@override _AppLifeCyclePageState createState() => _AppLifeCyclePageState();}class _AppLifeCyclePageState extends State<AppLifeCyclePage> with WidgetsBindingObserver {
@override void initState() {
super.initState(); /// If you want to monitor the application lifecycle , First bind the observer , /// When the binding is complete , If the application lifecycle changes , /// It's going to come back didChangeAppLifecycleState Method ; WidgetsBinding.instance.addObserver(this); } @override Widget build(BuildContext context) {
return Scaffold( appBar: AppBar( // title title: Text(" Application life cycle "), // Return button leading: BackButton(), ), body: Container( // Center setting alignment: Alignment.center, child: Text(" Application life cycle "), ), ); } /// Remove the observer registered in the component @override void dispose() {
super.dispose(); WidgetsBinding.instance.removeObserver(this); } /// When the application lifecycle changes , The method is called back @override void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state); print(" Current application lifecycle state : ${state}"); if(state == AppLifecycleState.paused){
print(" The app goes into the background paused"); }else if(state == AppLifecycleState.resumed){
print(" Application enters the front desk resumed"); }else if(state == AppLifecycleState.inactive){
// App goes inactive , Tathagata made a phone call , The phone application enters the front desk // This application enters this state print(" App goes inactive inactive"); }else if(state == AppLifecycleState.detached){
// Applications are still Flutter Running on the engine , But with the host View Component separation print(" The application enters detached state detached"); } }}
Running effect :
Enter interface : After entering the interface , Because I haven't registered WidgetsBindingObserver , So do not print the log , After the interface is loaded , All kinds of operations ( If pressed Home key , Press the back button , Press the menu key ) Only when the log is printed ;
Press the menu key :
- Interface state :
- Print log :
I/flutter (30370): Current application lifecycle state : AppLifecycleState.inactiveI/flutter (30370): App goes inactive inactiveD/FlutterView(30370): Detaching from a FlutterEngine: io.flutter.embedding.engine.FlutterEngine@9d12774I/flutter (30370): Current application lifecycle state : AppLifecycleState.pausedI/flutter (30370): The app goes into the background paused
Click again to return to the application interface :
- Interface state :
- Print log :
I/flutter (30370): Current application lifecycle state : AppLifecycleState.resumedI/flutter (30370): Application enters the front desk resumedD/FlutterView(30370): Attaching to a FlutterEngine: io.flutter.embedding.engine.FlutterEngine@9d12774
Full log :
# Press the menu key I/flutter (30370): Current application lifecycle state : AppLifecycleState.inactiveI/flutter (30370): App goes inactive inactiveD/FlutterView(30370): Detaching from a FlutterEngine: io.flutter.embedding.engine.FlutterEngine@9d12774I/flutter (30370): Current application lifecycle state : AppLifecycleState.pausedI/flutter (30370): The app goes into the background paused# Go back to the screen I/flutter (30370): Current application lifecycle state : AppLifecycleState.resumedI/flutter (30370): Application enters the front desk resumedD/FlutterView(30370): Attaching to a FlutterEngine: io.flutter.embedding.engine.FlutterEngine@9d12774
Four 、 Related resources
Reference material :
- Flutter Official website : https://flutter.dev/
- Flutter Plug in download address : https://pub.dev/packages
- Flutter Developing documents : https://flutter.cn/docs ( Strongly recommend )
- official GitHub Address : https://github.com/flutter
- Flutter The Chinese community : https://flutter.cn/
- Flutter Practical tutorial : https://flutter.cn/docs/cookbook
- Flutter CodeLab : https://codelabs.flutter-io.cn/
- Dart Chinese document : https://dart.cn/
- Dart Developer website : https://api.dart.dev/
- Flutter Chinese net ( unofficial , The translation is very good ) : https://flutterchina.club/ , http://flutter.axuer.com/docs/
- Flutter Related issues : https://flutterchina.club/faq/ ( It is recommended to watch it at the introductory stage )
Blog source download :
GitHub Address : https://github.com/han1202012/flutter_cmd ( Keep updating with the progress of the blog , There may not be the source code of this blog )
Blog source snapshot : https://download.csdn.net/download/han1202012/15547438 ( The source code snapshot of this blog , You can find the source code of this blog )
边栏推荐
- Technical solution of vision and manipulator calibration system
- MySQL learning record (5)
- 《Just because》阅读感受
- B.Odd Swap Sort(Codeforces Round #771 (Div. 2))
- MySQL learning record (8)
- Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale
- [Jianzhi offer] 57 And are two numbers of S
- How do I access the kubernetes API?
- *C语言期末课程设计*——通讯录管理系统(完整项目+源代码+详细注释)
- MySQL learning record (7)
猜你喜欢
LightGBM原理及天文数据中的应用
pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform
pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
CVPR论文解读 | 弱监督的高保真服饰模特生成
发现你看不到的物体!南开&武大&ETH提出用于伪装目标检测SINet,代码已开源!...
读博士吧,研究奶牛的那种!鲁汶大学 Livestock Technology 组博士招生,牛奶质量监测...
A specially designed loss is used to deal with data sets with unbalanced categories
MySQL learning record (2)
MySQL learning record (6)
Introduction to victoriametrics
随机推荐
TinyMCE visual editor adds Baidu map plug-in
Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file
Jar package startup failed -mysql modify the default port number / set password free enter
pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform
SQL必需掌握的100个重要知识点:使用游标
Unity3D学习笔记4——创建Mesh高级接口
The difference between include < > and include ""
What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together
Share how to make professional hand drawn electronic maps
Introduction to victoriametrics
Capacity expansion mechanism of ArrayList
From personal heroes to versatile developers, the era of programmer 3.0 is coming
Servicemesh mainly solves three pain points
一周生活
GEE:(二)对影像进行重采样
Introduction to the principle of geographical detector
VictoriaMetrics 简介
What is it that makes you tremble? Those without fans can learn
【C 题集】of Ⅴ
2019 Nanchang (relive the classic)