0
0

More than 3 years have passed since last update.

[Flutter] アプリがフォアグランドになった時のイベントを検知する

Posted at

やること

  1. StatefullWidgetを使用する
  2. with WidgetsBindingObserver
  3. initState()に処理を追加
  4. didChangeAppLifecycleState()をoverride
  5. disposeの設定をする

内容

class SampleClass extends StatefulWidget {
  SampleClass({Key key}) : super(key: key);

  @override
  _SampleClassState createState() => _SampleClassState();
}

// 1. with WidgetsBindingObserver
class _SampleClassState extends State<SampleClass> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    // 2. initState()に処理を追加
    WidgetsBinding.instance.addObserver(this);
    });
  }

  // 4. disposeの設定をする
  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  // 3. didChangeAppLifecycleState()をoverride
  @override
    Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
      if (state == AppLifecycleState.resumed) {
         // TODO: フォアグランドに来たときの処理
      }
    }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0