{"id":354297,"date":"2024-05-20T22:45:37","date_gmt":"2024-05-20T22:45:37","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=354297"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=354297","title":{"rendered":"<span>\u041d\u043e\u0432\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u043d\u0438\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432\u043e Flutter<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>&#171;\u0421\u0442\u0430\u0440\u044b\u0439&#187; \u0441\u043f\u043e\u0441\u043e\u0431<\/h3>\n<p>\u0414\u043e Flutter 3.13 \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u043c\u043e\u0436\u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0438\u043a\u0441\u0438\u043d\u0430 <code>WidgetsBindingObserver<\/code>.\u00a0\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0431\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c <code>WidgetsBindingObserver<\/code> \u0432 \u043a\u043b\u0430\u0441\u0441 <code>State<\/code>\u00a0\u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c <code>didChangeAppLifecycleState<\/code> \u043c\u0435\u0442\u043e\u0434.\u00a0\u0412 <code>idChangeAppLifecycleState<\/code>\u00a0\u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f (<code>AppLifecycleState<\/code>).<\/p>\n<pre><code class=\"dart\">class AppLifecyclePageOld extends StatefulWidget {   const AppLifecyclePageOld({super.key});    @override   State&lt;AppLifecyclePageOld> createState() => _AppLifecyclePageOldState(); }  class _AppLifecyclePageOldState extends State&lt;AppLifecyclePageOld>     \/\/ Use the WidgetsBindingObserver mixin     with WidgetsBindingObserver {   @override   void initState() {     super.initState();      \/\/ Register your State class as a binding observer     WidgetsBinding.instance.addObserver(this);   }    @override   void dispose() {     \/\/ Unregister your State class as a binding observer     WidgetsBinding.instance.removeObserver(this);      super.dispose();   }    \/\/ Override the didChangeAppLifecycleState method and   \/\/ listen to the app lifecycle state changes   @override   void didChangeAppLifecycleState(AppLifecycleState state) {     super.didChangeAppLifecycleState(state);      switch (state) {       case AppLifecycleState.detached:         _onDetached();       case AppLifecycleState.resumed:         _onResumed();       case AppLifecycleState.inactive:         _onInactive();       case AppLifecycleState.hidden:         _onHidden();       case AppLifecycleState.paused:         _onPaused();     }   }    void _onDetached() => print('detached');    void _onResumed() => print('resumed');    void _onInactive() => print('inactive');    void _onHidden() => print('hidden');    void _onPaused() => print('paused');    @override   Widget build(BuildContext context) {     return const Scaffold(       body: Placeholder(),     );   } }<\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043d\u043e\u0432\u044b\u0439\u00a0\u043a\u043b\u0430\u0441\u0441 <code>AppLifecycleListener<\/code>.  <\/p>\n<blockquote>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, &#171;\u0441\u0442\u0430\u0440\u044b\u0439&#187; \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u043d\u0438\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u0432\u0441\u0435 \u0435\u0449\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d. \u0424\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438, <code>AppLifecycleListener<\/code>  \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043c\u0438\u043a\u0441\u0438\u043d <code>WidgetsBindingObserver<\/code>.  <\/p>\n<\/blockquote>\n<h3>&#171;\u041d\u043e\u0432\u044b\u0439&#187; \u0441\u043f\u043e\u0441\u043e\u0431<\/h3>\n<p><code>AppLifecycleListener<\/code>\u00a0&#8212; \u044d\u0442\u043e \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u043d\u0438\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0431\u0435\u0437 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0438\u043a\u0441\u0438\u043d\u00a0<code>WidgetsBindingObserver<\/code>\u00a0.\u00a0\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 \u00a0<code>AppLifecycleListener<\/code>\u00a0 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0439\u0442\u0435 \u0432\u0441\u0435 \u043a\u043e\u043b\u0431\u044d\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0430\u0442\u044c.<\/p>\n<pre><code class=\"dart\">class AppLifecyclePage extends StatefulWidget {   const AppLifecyclePage({super.key});    @override   State&lt;AppLifecyclePage> createState() => _AppLifecyclePageState(); }  class _AppLifecyclePageState extends State&lt;AppLifecyclePage> {   late final AppLifecycleListener _listener;    @override   void initState() {     super.initState();      \/\/ Initialize the AppLifecycleListener class and pass callbacks     _listener = AppLifecycleListener(       onStateChange: _onStateChanged,     );   }    @override   void dispose() {     \/\/ Do not forget to dispose the listener     _listener.dispose();      super.dispose();   }    \/\/ Listen to the app lifecycle state changes   void _onStateChanged(AppLifecycleState state) {     switch (state) {       case AppLifecycleState.detached:         _onDetached();       case AppLifecycleState.resumed:         _onResumed();       case AppLifecycleState.inactive:         _onInactive();       case AppLifecycleState.hidden:         _onHidden();       case AppLifecycleState.paused:         _onPaused();     }   }    void _onDetached() => print('detached');    void _onResumed() => print('resumed');    void _onInactive() => print('inactive');    void _onHidden() => print('hidden');    void _onPaused() => print('paused');    @override   Widget build(BuildContext context) {     return const Scaffold(       body: Placeholder(),     );   } }<\/code><\/pre>\n<h3>\u0415\u0441\u0442\u044c \u043b\u0438 \u0440\u0430\u0437\u043d\u0438\u0446\u0430?<\/h3>\n<p>\u041a\u0430\u043a \u043c\u043e\u0436\u0435\u0442\u0435 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, &#171;\u0441\u0442\u0430\u0440\u044b\u0439&#187; \u0438 &#171;\u043d\u043e\u0432\u044b\u0439&#187; \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u043d\u0438\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0438.\u00a0\u041e\u0434\u043d\u0430\u043a\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043d\u044f\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u043e\u0435 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u00a0 \u043a\u043b\u0430\u0441\u0441\u0430 <code>AppLifecycleListener<\/code>\u00a0, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0443 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0433\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0430 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f Flutter:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b8f\/05f\/534\/b8f05f53488d4ae4c401ef637e26dab1.png\" width=\"1000\" height=\"360\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b8f\/05f\/534\/b8f05f53488d4ae4c401ef637e26dab1.png\"\/><\/figure>\n<p>\u041d\u0430 \u044d\u0442\u043e\u0439 \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f Flutter.\u00a0\u0421\u0442\u0440\u0435\u043b\u043a\u0438 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u044b \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f\u043c\u0438.\u00a0\u041f\u0440\u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u00a0<code>didChangeAppLifecycleState<\/code>\u00a0(&#171;\u0441\u0442\u0430\u0440\u044b\u0439&#187; \u0441\u043f\u043e\u0441\u043e\u0431) \u0432\u044b \u043c\u043e\u0433\u043b\u0438 \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0433\u0434\u0430 \u0432\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0448\u043b\u043e \u0432\u00a0<code>resumed<\/code>\u00a0\u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435.\u00a0\u041e\u0434\u043d\u0430\u043a\u043e \u0432\u044b \u043d\u0435 \u0441\u043c\u043e\u0433\u043b\u0438 \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u044b \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f\u043c\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043b\u043e \u043b\u0438 \u0432\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0432\u00a0<code>resumed<\/code>\u00a0\u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438\u0437 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f\u00a0<code>inactive<\/code>\u00a0\u0438\u043b\u0438\u00a0<code>detached<\/code>.\u00a0\u0422\u0435\u043f\u0435\u0440\u044c \u043a\u043b\u0430\u0441\u0441\u00a0<code>AppLifecycleListener<\/code>\u00a0 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c:<\/p>\n<pre><code class=\"dart\">class AppLifecyclePage extends StatefulWidget {   const AppLifecyclePage({super.key});    @override   State&lt;AppLifecyclePage> createState() => _AppLifecyclePageState(); }  class _AppLifecyclePageState extends State&lt;AppLifecyclePage> {   late final AppLifecycleListener _listener;    @override   void initState() {     super.initState();      \/\/ Pass all the callbacks for the transitions you want to listen to     _listener = AppLifecycleListener(       onDetach: _onDetach,       onHide: _onHide,       onInactive: _onInactive,       onPause: _onPause,       onRestart: _onRestart,       onResume: _onResume,       onShow: _onShow,       onStateChange: _onStateChanged,     );   }    @override   void dispose() {     _listener.dispose();      super.dispose();   }    void _onDetach() => print('onDetach');    void _onHide() => print('onHide');    void _onInactive() => print('onInactive');    void _onPause() => print('onPause');    void _onRestart() => print('onRestart');    void _onResume() => print('onResume');    void _onShow() => print('onShow');    void _onStateChanged(AppLifecycleState state) {     \/\/ Track state changes   }    @override   Widget build(BuildContext context) {     return const Scaffold(       body: Placeholder(),     );   } }<\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0433\u043b\u0430\u0432\u043d\u043e\u0435 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e \u043a\u043b\u0430\u0441\u0441\u0430\u00a0<code>AppLifecycleListener<\/code>.\u00a0\u041e\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u044b \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f\u043c\u0438 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u043a\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0445 \u0432\u0430\u0441 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432.<\/p>\n<p>\u041d\u043e \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u043d\u0430 \u0432\u0435\u0449\u044c&#8230;<\/p>\n<h3>\u041a\u043e\u043b\u0431\u044d\u043a onExitRequested<\/h3>\n<p>\u0423 \u043a\u043b\u0430\u0441\u0441\u0430 <code>AppLifecycleListener<\/code>\u00a0\u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043a\u043e\u043b\u0431\u044d\u043a\u00a0<code>onExitRequested<\/code>.\u00a0\u041e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043b\u0438 \u043e\u043d\u043e \u0432\u044b\u0439\u0442\u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b\u0445\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c.\u00a0\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 macOS, \u0433\u0434\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u043a\u0440\u044b\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/031\/508\/e34\/031508e34f2c510a092b7dd489a6063f.gif\" width=\"600\" height=\"475\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/031\/508\/e34\/031508e34f2c510a092b7dd489a6063f.gif\"\/><\/figure>\n<p>\u0427\u0442\u043e\u0431\u044b \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434, \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0437 \u043a\u043e\u043b\u0431\u044d\u043a\u0430 \u00a0<code>onExitRequested<\/code>\u00a0 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>AppExitResponse.cancel<\/code>.\u00a0\u0412 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u0435\u0440\u043d\u0438\u0442\u0435 <code>AppExitResponse.exit<\/code>, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443.  <\/p>\n<pre><code class=\"dart\">class AppLifecyclePage extends StatefulWidget {   const AppLifecyclePage({super.key});    @override   State&lt;AppLifecyclePage> createState() => _AppLifecyclePageState(); }  class _AppLifecyclePageState extends State&lt;AppLifecyclePage> {   late final AppLifecycleListener _listener;    @override   void initState() {     super.initState();      _listener = AppLifecycleListener(       \/\/ Handle the onExitRequested callback       onExitRequested: _onExitRequested,     );   }    @override   void dispose() {     _listener.dispose();      super.dispose();   }    \/\/ Ask the user if they want to exit the app. If the user   \/\/ cancels the exit, return AppExitResponse.cancel. Otherwise,   \/\/ return AppExitResponse.exit.   Future&lt;AppExitResponse> _onExitRequested() async {     final response = await showDialog&lt;AppExitResponse>(       context: context,       barrierDismissible: false,       builder: (context) => AlertDialog.adaptive(         title: const Text('Are you sure you want to quit this app?'),         content: const Text('All unsaved progress will be lost.'),         actions: [           TextButton(             child: const Text('Cancel'),             onPressed: () {               Navigator.of(context).pop(AppExitResponse.cancel);             },           ),           TextButton(             child: const Text('Ok'),             onPressed: () {               Navigator.of(context).pop(AppExitResponse.exit);             },           ),         ],       ),     );      return response ?? AppExitResponse.exit;   }    @override   Widget build(BuildContext context) {     return Scaffold(       appBar: AppBar(         title: const Text('App Lifecycle Demo'),       ),       body: Center(         child: Text(           '?',           style: Theme.of(context).textTheme.displayLarge,         ),       ),     );   } }<\/code><\/pre>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u041a\u043b\u0430\u0441\u0441  <code>AppLifecycleListener<\/code>\u00a0&#8212; \u044d\u0442\u043e \u043d\u043e\u0432\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438, \u0447\u0442\u043e \u0431\u043e\u043b\u0435\u0435 \u0432\u0430\u0436\u043d\u043e, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438.\u00a0\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043a\u043e\u043b\u0431\u044d\u043a <code>onExitRequested<\/code>  \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434 \u0432 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b\u0445\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c.\u00a0\u042f \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u0432\u044b \u043d\u0430\u0448\u043b\u0438 \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f\u00a0<code>AppLifecycleListener<\/code>\u00a0\u043a\u043b\u0430\u0441\u0441\u0430 \u0432 \u0432\u0430\u0448\u0438\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 Flutter.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/759628\/\"> https:\/\/habr.com\/ru\/articles\/759628\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>&#171;\u0421\u0442\u0430\u0440\u044b\u0439&#187; \u0441\u043f\u043e\u0441\u043e\u0431<\/h3>\n<p>\u0414\u043e Flutter 3.13 \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u043c\u043e\u0436\u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0438\u043a\u0441\u0438\u043d\u0430 <code>WidgetsBindingObserver<\/code>.\u00a0\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0431\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c <code>WidgetsBindingObserver<\/code> \u0432 \u043a\u043b\u0430\u0441\u0441 <code>State<\/code>\u00a0\u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c <code>didChangeAppLifecycleState<\/code> \u043c\u0435\u0442\u043e\u0434.\u00a0\u0412 <code>idChangeAppLifecycleState<\/code>\u00a0\u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f (<code>AppLifecycleState<\/code>).<\/p>\n<pre><code class=\"dart\">class AppLifecyclePageOld extends StatefulWidget {   const AppLifecyclePageOld({super.key});    @override   State&lt;AppLifecyclePageOld> createState() => _AppLifecyclePageOldState(); }  class _AppLifecyclePageOldState extends State&lt;AppLifecyclePageOld>     \/\/ Use the WidgetsBindingObserver mixin     with WidgetsBindingObserver {   @override   void initState() {     super.initState();      \/\/ Register your State class as a binding observer     WidgetsBinding.instance.addObserver(this);   }    @override   void dispose() {     \/\/ Unregister your State class as a binding observer     WidgetsBinding.instance.removeObserver(this);      super.dispose();   }    \/\/ Override the didChangeAppLifecycleState method and   \/\/ listen to the app lifecycle state changes   @override   void didChangeAppLifecycleState(AppLifecycleState state) {     super.didChangeAppLifecycleState(state);      switch (state) {       case AppLifecycleState.detached:         _onDetached();       case AppLifecycleState.resumed:         _onResumed();       case AppLifecycleState.inactive:         _onInactive();       case AppLifecycleState.hidden:         _onHidden();       case AppLifecycleState.paused:         _onPaused();     }   }    void _onDetached() => print('detached');    void _onResumed() => print('resumed');    void _onInactive() => print('inactive');    void _onHidden() => print('hidden');    void _onPaused() => print('paused');    @override   Widget build(BuildContext context) {     return const Scaffold(       body: Placeholder(),     );   } }<\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043d\u043e\u0432\u044b\u0439\u00a0\u043a\u043b\u0430\u0441\u0441 <code>AppLifecycleListener<\/code>.  <\/p>\n<blockquote>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, &#171;\u0441\u0442\u0430\u0440\u044b\u0439&#187; \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u043d\u0438\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u0432\u0441\u0435 \u0435\u0449\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d. \u0424\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438, <code>AppLifecycleListener<\/code>  \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043c\u0438\u043a\u0441\u0438\u043d <code>WidgetsBindingObserver<\/code>.  <\/p>\n<\/blockquote>\n<h3>&#171;\u041d\u043e\u0432\u044b\u0439&#187; \u0441\u043f\u043e\u0441\u043e\u0431<\/h3>\n<p><code>AppLifecycleListener<\/code>\u00a0&#8212; \u044d\u0442\u043e \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u043d\u0438\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0431\u0435\u0437 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0438\u043a\u0441\u0438\u043d\u00a0<code>WidgetsBindingObserver<\/code>\u00a0.\u00a0\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 \u00a0<code>AppLifecycleListener<\/code>\u00a0 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0439\u0442\u0435 \u0432\u0441\u0435 \u043a\u043e\u043b\u0431\u044d\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0430\u0442\u044c.<\/p>\n<pre><code class=\"dart\">class AppLifecyclePage extends StatefulWidget {   const AppLifecyclePage({super.key});    @override   State&lt;AppLifecyclePage> createState() => _AppLifecyclePageState(); }  class _AppLifecyclePageState extends State&lt;AppLifecyclePage> {   late final AppLifecycleListener _listener;    @override   void initState() {     super.initState();      \/\/ Initialize the AppLifecycleListener class and pass callbacks     _listener = AppLifecycleListener(       onStateChange: _onStateChanged,     );   }    @override   void dispose() {     \/\/ Do not forget to dispose the listener     _listener.dispose();      super.dispose();   }    \/\/ Listen to the app lifecycle state changes   void _onStateChanged(AppLifecycleState state) {     switch (state) {       case AppLifecycleState.detached:         _onDetached();       case AppLifecycleState.resumed:         _onResumed();       case AppLifecycleState.inactive:         _onInactive();       case AppLifecycleState.hidden:         _onHidden();       case AppLifecycleState.paused:         _onPaused();     }   }    void _onDetached() => print('detached');    void _onResumed() => print('resumed');    void _onInactive() => print('inactive');    void _onHidden() => print('hidden');    void _onPaused() => print('paused');    @override   Widget build(BuildContext context) {     return const Scaffold(       body: Placeholder(),     );   } }<\/code><\/pre>\n<h3>\u0415\u0441\u0442\u044c \u043b\u0438 \u0440\u0430\u0437\u043d\u0438\u0446\u0430?<\/h3>\n<p>\u041a\u0430\u043a \u043c\u043e\u0436\u0435\u0442\u0435 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, &#171;\u0441\u0442\u0430\u0440\u044b\u0439&#187; \u0438 &#171;\u043d\u043e\u0432\u044b\u0439&#187; \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u043d\u0438\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0438.\u00a0\u041e\u0434\u043d\u0430\u043a\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043d\u044f\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u043e\u0435 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u00a0 \u043a\u043b\u0430\u0441\u0441\u0430 <code>AppLifecycleListener<\/code>\u00a0, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0443 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0433\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0430 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f Flutter:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041d\u0430 \u044d\u0442\u043e\u0439 \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f Flutter.\u00a0\u0421\u0442\u0440\u0435\u043b\u043a\u0438 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u044b \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f\u043c\u0438.\u00a0\u041f\u0440\u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u00a0<code>didChangeAppLifecycleState<\/code>\u00a0(&#171;\u0441\u0442\u0430\u0440\u044b\u0439&#187; \u0441\u043f\u043e\u0441\u043e\u0431) \u0432\u044b \u043c\u043e\u0433\u043b\u0438 \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0433\u0434\u0430 \u0432\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0448\u043b\u043e \u0432\u00a0<code>resumed<\/code>\u00a0\u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435.\u00a0\u041e\u0434\u043d\u0430\u043a\u043e \u0432\u044b \u043d\u0435 \u0441\u043c\u043e\u0433\u043b\u0438 \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u044b \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f\u043c\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043b\u043e \u043b\u0438 \u0432\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0432\u00a0<code>resumed<\/code>\u00a0\u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438\u0437 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f\u00a0<code>inactive<\/code>\u00a0\u0438\u043b\u0438\u00a0<code>detached<\/code>.\u00a0\u0422\u0435\u043f\u0435\u0440\u044c \u043a\u043b\u0430\u0441\u0441\u00a0<code>AppLifecycleListener<\/code>\u00a0 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c:<\/p>\n<pre><code class=\"dart\">class AppLifecyclePage extends StatefulWidget {   const AppLifecyclePage({super.key});    @override   State&lt;AppLifecyclePage> createState() => _AppLifecyclePageState(); }  class _AppLifecyclePageState extends State&lt;AppLifecyclePage> {   late final AppLifecycleListener _listener;    @override   void initState() {     super.initState();      \/\/ Pass all the callbacks for the transitions you want to listen to     _listener = AppLifecycleListener(       onDetach: _onDetach,       onHide: _onHide,       onInactive: _onInactive,       onPause: _onPause,       onRestart: _onRestart,       onResume: _onResume,       onShow: _onShow,       onStateChange: _onStateChanged,     );   }    @override   void dispose() {     _listener.dispose();      super.dispose();   }    void _onDetach() => print('onDetach');    void _onHide() => print('onHide');    void _onInactive() => print('onInactive');    void _onPause() => print('onPause');    void _onRestart() => print('onRestart');    void _onResume() => print('onResume');    void _onShow() => print('onShow');    void _onStateChanged(AppLifecycleState state) {     \/\/ Track state changes   }    @override   Widget build(BuildContext context) {     return const Scaffold(       body: Placeholder(),     );   } }<\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0433\u043b\u0430\u0432\u043d\u043e\u0435 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e \u043a\u043b\u0430\u0441\u0441\u0430\u00a0<code>AppLifecycleListener<\/code>.\u00a0\u041e\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u044b \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f\u043c\u0438 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u043a\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0445 \u0432\u0430\u0441 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432.<\/p>\n<p>\u041d\u043e \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u043d\u0430 \u0432\u0435\u0449\u044c&#8230;<\/p>\n<h3>\u041a\u043e\u043b\u0431\u044d\u043a onExitRequested<\/h3>\n<p>\u0423 \u043a\u043b\u0430\u0441\u0441\u0430 <code>AppLifecycleListener<\/code>\u00a0\u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043a\u043e\u043b\u0431\u044d\u043a\u00a0<code>onExitRequested<\/code>.\u00a0\u041e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043b\u0438 \u043e\u043d\u043e \u0432\u044b\u0439\u0442\u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b\u0445\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c.\u00a0\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 macOS, \u0433\u0434\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u043a\u0440\u044b\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0427\u0442\u043e\u0431\u044b \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434, \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0437 \u043a\u043e\u043b\u0431\u044d\u043a\u0430 \u00a0<code>onExitRequested<\/code>\u00a0 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>AppExitResponse.cancel<\/code>.\u00a0\u0412 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u0435\u0440\u043d\u0438\u0442\u0435 <code>AppExitResponse.exit<\/code>, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443.  <\/p>\n<pre><code class=\"dart\">class AppLifecyclePage extends StatefulWidget {   const AppLifecyclePage({super.key});    @override   State&lt;AppLifecyclePage> createState() => _AppLifecyclePageState(); }  class _AppLifecyclePageState extends State&lt;AppLifecyclePage> {   late final AppLifecycleListener _listener;    @override   void initState() {     super.initState();      _listener = AppLifecycleListener(       \/\/ Handle the onExitRequested callback       onExitRequested: _onExitRequested,     );   }    @override   void dispose() {     _listener.dispose();      super.dispose();   }    \/\/ Ask the user if they want to exit the app. If the user   \/\/ cancels the exit, return AppExitResponse.cancel. Otherwise,   \/\/ return AppExitResponse.exit.   Future&lt;AppExitResponse> _onExitRequested() async {     final response = await showDialog&lt;AppExitResponse>(       context: context,       barrierDismissible: false,       builder: (context) => AlertDialog.adaptive(         title: const Text('Are you sure you want to quit this app?'),         content: const Text('All unsaved progress will be lost.'),         actions: [           TextButton(             child: const Text('Cancel'),             onPressed: () {               Navigator.of(context).pop(AppExitResponse.cancel);             },           ),           TextButton(             child: const Text('Ok'),             onPressed: () {               Navigator.of(context).pop(AppExitResponse.exit);             },           ),         ],       ),     );      return response ?? AppExitResponse.exit;   }    @override   Widget build(BuildContext context) {     return Scaffold(       appBar: AppBar(         title: const Text('App Lifecycle Demo'),       ),       body: Center(         child: Text(           '?',           style: Theme.of(context).textTheme.displayLarge,         ),       ),     );   } }<\/code><\/pre>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u041a\u043b\u0430\u0441\u0441  <code>AppLifecycleListener<\/code>\u00a0&#8212; \u044d\u0442\u043e \u043d\u043e\u0432\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438, \u0447\u0442\u043e \u0431\u043e\u043b\u0435\u0435 \u0432\u0430\u0436\u043d\u043e, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438.\u00a0\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043a\u043e\u043b\u0431\u044d\u043a <code>onExitRequested<\/code>  \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434 \u0432 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b\u0445\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c.\u00a0\u042f \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u0432\u044b \u043d\u0430\u0448\u043b\u0438 \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f\u00a0<code>AppLifecycleListener<\/code>\u00a0\u043a\u043b\u0430\u0441\u0441\u0430 \u0432 \u0432\u0430\u0448\u0438\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 Flutter.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/759628\/\"> https:\/\/habr.com\/ru\/articles\/759628\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-354297","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/354297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=354297"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/354297\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=354297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=354297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=354297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}