The Lifecycle State Machine
The Android OS manages the lifecycle of every Activity using a 'stack' approach. As the user moves between screens or receives a phone call, the OS moves your Activity through a series of callback methods. For a Lead Architect, the goal is to ensure that heavy resources (like AI model listeners) are only active when the user can actually see the screen.
- The Visible Lifecycle (onStart to onStop)
Between onStart() and onStop(), the Activity is visible to the user. This is the window where you should maintain active UI updates. However, the Activity might not be in the 'foreground' (interactive) during this entire window.
- onCreate(): The 'Initialization' phase. Used to inflate layouts and initialize ViewModels. In Flutter, this is where the Engine is usually warmed up.
- onStart(): The 'Visibility' phase. The app is on-screen but might be covered by a dialog.
- onResume(): The 'Interactive' phase. The app is in the foreground. Start your camera previews or high-frequency AI polling here.
- The Background Transition (onPause to onDestroy)
When the user leaves the app, the reverse happens. onPause() is the most critical callback for a Lead Developer—it is the last guaranteed moment to save small pieces of state before the OS might kill the process.
- onPause(): The user is leaving (e.g., another app is opening in split-screen). Stop animations or sensor data collection here.
- onStop(): The app is no longer visible. You must release heavy resources now to prevent being the first app the OS kills to save RAM.
- onDestroy(): The final cleanup. Release memory and shut down background threads. Note: This is NOT always called if the OS kills the process abruptly.
- Handling Process Death
In Chennai's competitive mid-range device market, RAM management is strict. If a user backgrounds Revochamp to check a message, the OS might kill the process. To handle this, you must use SavedStateHandle in your ViewModels to store 'Transient State' so the user can pick up exactly where they left off in the AI builder.
Lifecycle Mapping: Native vs. Flutter
| Android Native State | Description | Flutter AppLifecycleState |
|---|---|---|
| onResume() | App is focused and interactive. | AppLifecycleState.resumed |
| onPause() | App is partially visible / losing focus. | AppLifecycleState.inactive |
| onStop() | App is completely in the background. | AppLifecycleState.hidden / paused |
| onDestroy() | App is being terminated. | AppLifecycleState.detached |
| Process Death | OS kills app for memory. | N/A (Requires manual persistence) |