본문 바로가기
개발/Flutter

Flutter 플러터 앱 상태 확인하기 AppLifecycleState

by SPNK 2022. 12. 24.
반응형
  • 예시 코드
import 'package:flutter/material.dart';

class ExamplePage extends StatefulWidget {
  const ExamplePage({Key? key}) : super(key: key);

  @override
  State<ExamplePage> createState() => _ExamplePageState();
}

class _ExamplePageState extends State<ExamplePage> with WidgetsBindingObserver {
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        print("앱이 표시되고 사용자 입력에 응답합니다");
        break;
      case AppLifecycleState.inactive:
        print("앱이 비활성화 상태이고 사용자의 입력을 받지 않습니다");
        break;
      case AppLifecycleState.detached:
        print("모든 뷰가 제거되고 플러터 엔진만 동작 중이며 앱이 종료되기 직전에 실행됩니다");
        break;
      case AppLifecycleState.paused:
        print("앱이 현재 사용자에게 보이지 않고, 사용자의 입력을 받지 않으며, 백그라운드에서 동작 중입니다");
        break;
    }
  }

  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    super.dispose();

    WidgetsBinding.instance.removeObserver(this);
  }

  Widget build(BuildContext context) {
    return Scaffold();
  }
}
반응형

댓글