What are all the app lifecycle states in flutter ?
#flutter#state
Answer
App Lifecycle States in Flutter
AppLifecycleState defines the state of the application:
States
resumed
App is visible and accepting user input (foreground).
dartif (state == AppLifecycleState.resumed) { // Resume timers, API calls, animations startTimer(); }
inactive
App is visible but not accepting input (iOS only, when another app is displayed over it).
dartif (state == AppLifecycleState.inactive) { // Pause animations, heavy operations pauseAnimation(); }
paused
App is not visible (background, locked screen, another app in foreground).
dartif (state == AppLifecycleState.paused) { // Pause everything, save state saveState(); pauseAudio(); }
detached
App is about to be terminated (rare, usually not used).
dartif (state == AppLifecycleState.detached) { // Final cleanup cleanup(); }
Implementation
dartclass MyApp extends StatefulWidget { _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> with WidgetsBindingObserver { void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); } void didChangeAppLifecycleState(AppLifecycleState state) { print('Lifecycle: $state'); switch (state) { case AppLifecycleState.resumed: print('App resumed'); break; case AppLifecycleState.inactive: print('App inactive'); break; case AppLifecycleState.paused: print('App paused'); break; case AppLifecycleState.detached: print('App detached'); break; } } void dispose() { WidgetsBinding.instance.removeObserver(this); super.dispose(); } Widget build(BuildContext context) => MaterialApp(home: HomeScreen()); }
Lifecycle Flow
textApp Starts: resumed ↓ Press Home/Another App: inactive → paused ↓ Switch Back to App: resumed ↓ Close App: detached
Use Case: Save data on pause, resume timers on resume.