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();
}
}