import 'package:flutter/material.dart'; void main() => runApp(LifecycleDemo()); // リスト 5.12 class LifecycleDemo extends StatefulWidget { const LifecycleDemo({Key? key}) : super(key: key); @override _LifecycleDemoState createState() => _LifecycleDemoState(); } class _LifecycleDemoState extends State<LifecycleDemo> with WidgetsBindingObserver { @override void initState() { super.initState(); WidgetsBinding.instance?.addObserver(this); } @override void dispose() { WidgetsBinding.instance?.removeObserver(this); super.dispose(); } @override void didChangeAppLifecycleState(AppLifecycleState state) { // print('AppLifecycleState: $state'); if (state == AppLifecycleState.inactive || state == AppLifecycleState.paused) { // 表示中の画像を保存する処理を記載 } } @override Widget build(BuildContext context) { return MaterialApp( title: 'Lifecycle', home: Scaffold( appBar: AppBar( title: const Text('Lifecycle'), ), body: Center(child: Text('ライフサイクル')) ) ); } }