# App Suspension Implementation ## Overview This document describes the implementation of automatic app suspension when the screen turns off or the app goes to the background, ensuring optimal battery usage and proper resource management. ## Implementation Details ### Core Components #### 1. AppLifecycleManager (`lib/utils/app_lifecycle_manager.dart`) A dedicated utility class that manages app lifecycle events: - **Singleton Pattern**: Uses a singleton instance to ensure global access - **WidgetsBindingObserver**: Implements Flutter's lifecycle observer interface - **Game Registration**: Manages the currently active game instance - **Automatic Suspension**: Pauses game when app goes to background - **Automatic Resumption**: Resumes game when app returns to foreground #### 2. Main App Integration (`lib/main.dart`) - Initializes the `AppLifecycleManager` during app startup - Ensures lifecycle management is active throughout the app's lifetime #### 3. Game Screen Integration (`lib/ui/game_screen.dart`) - Registers the active game instance when the game screen is created - Unregisters the game instance when the game screen is disposed - Ensures proper cleanup of lifecycle management ### Lifecycle States Handled The implementation responds to the following Flutter app lifecycle states: #### App Suspension Triggers: - `AppLifecycleState.inactive` - App is inactive (transitioning) - `AppLifecycleState.paused` - App is paused (background/screen off) - `AppLifecycleState.detached` - App is detached from engine - `AppLifecycleState.hidden` - App is hidden (iOS specific) #### App Resumption Trigger: - `AppLifecycleState.resumed` - App is active and visible ### Game Suspension Features When the app is suspended, the following actions occur: 1. **Game Pause**: [`game.pauseGame()`](lib/game/zentap_game.dart:378) is called which: - Sets `paused = true` on the Flame game engine - Sets `gameActive = false` to stop game logic updates - Pauses background music via [`audioManager.pauseBackgroundMusic()`](lib/game/audio/audio_manager.dart:231) - Stops tilt detection via [`_tiltDetector.stopListening()`](lib/utils/tilt_detector.dart:35) 2. **Resource Conservation**: - Game loop stops updating - Audio playback is paused - Sensor listening (accelerometer for tilt) is stopped - Battery usage is minimized ### Game Resumption Features When the app is resumed, the following actions occur: 1. **Game Resume**: [`game.resumeGame()`](lib/game/zentap_game.dart:385) is called which: - Sets `paused = false` on the Flame game engine - Sets `gameActive = true` to resume game logic updates - Resumes background music via [`audioManager.resumeBackgroundMusic()`](lib/game/audio/audio_manager.dart:241) - Restarts tilt detection via [`_initializeTiltDetection()`](lib/game/zentap_game.dart:113) 2. **Seamless Experience**: - Game state is preserved (score, timer, bubble positions) - Audio resumes from where it left off - Tilt detection is reactivated - User can continue playing immediately ## Benefits ### Battery Optimization - **Reduced CPU Usage**: Game loop and animations are paused - **Audio Power Savings**: Background music and sound effects are paused - **Sensor Power Savings**: Accelerometer monitoring is stopped - **GPU Savings**: Rendering is paused by the Flame engine ### User Experience - **State Preservation**: Game state is maintained during suspension - **Instant Resumption**: No loading or restart required when returning - **Automatic Management**: No user intervention required - **Cross-Platform**: Works on Android, iOS, and desktop platforms ### Resource Management - **Memory Efficiency**: Game objects remain in memory but stop processing - **Network Conservation**: Any network activity is paused - **System Resources**: Frees up CPU cycles for other apps ## Technical Architecture ``` App Lifecycle Event ↓ AppLifecycleManager.didChangeAppLifecycleState() ↓ Determines Current Game Instance ↓ Calls game.pauseGame() or game.resumeGame() ↓ Game Engine Handles: - Pause/Resume Game Loop - Audio Management - Sensor Management - Resource Conservation ``` ## Usage The app suspension feature is **automatic** and requires no user interaction: 1. **During Gameplay**: User is playing the game normally 2. **Screen Off/Background**: User turns off screen or switches to another app 3. **Automatic Suspension**: Game automatically pauses, conserving battery 4. **Return to App**: User returns to the app 5. **Automatic Resumption**: Game automatically resumes from exact same state ## Testing To test the app suspension feature: 1. **Manual Testing**: - Start a game session - Turn off the device screen or switch to another app - Wait a few seconds - Return to the app - Verify the game resumes seamlessly 2. **Debug Verification**: - Check console logs for pause/resume calls - Verify audio stops and starts appropriately - Confirm game timer stops and resumes correctly ## Future Enhancements Potential improvements for the suspension system: 1. **Suspension Analytics**: Track how often users suspend/resume 2. **Custom Suspension Messages**: Show user-friendly messages during suspension 3. **Progressive Suspension**: Gradually reduce resources over time 4. **Background Sync**: Sync game progress during suspension (if applicable) ## Conclusion The app suspension implementation ensures ZenTap is a responsible mobile app citizen by: - Conserving device battery when not in active use - Preserving user game state for seamless experience - Following Flutter and mobile platform best practices - Providing automatic, transparent resource management This feature enhances both user experience and device performance, making ZenTap a more polished and professional mobile application.