A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
Android cannot be forced to wait for asynchronous work in OnSleep once the user presses Overview and then “Close all”. When the task switcher’s “Close all” is used, Android is explicitly allowed to kill the app’s process; any in-flight background work in OnSleep or other lifecycle callbacks can be terminated at any time. .NET MAUI cannot override this behavior.
Key points from the MAUI lifecycle:
-
OnSleep(and the mappedOnPause/OnStopevents on Android) are notification points, not guaranteed execution windows. The OS may kill the app immediately afterward, especially when the user explicitly closes it. - The MAUI
Window.Stoppedevent maps to AndroidOnStop, and the docs explicitly note: “There’s no guarantee that an app will resume from this state, because it may be terminated by the operating system.” This also implies no guarantee that long-running work will complete.
Because of this, long-running or critical operations such as file uploads should not rely on finishing inside OnSleep or similar callbacks. Instead, use patterns that tolerate interruption, for example:
- Persist upload state locally (e.g., mark items as “pending upload”) and resume uploads next time the app starts (
OnCreate/OnApplicationCreateor app startup logic). - Use a proper background mechanism (e.g., a platform background service or scheduled job) that can continue independently of the UI process, if the scenario requires it and the platform permits.
In summary, Android cannot be instructed to wait for the Task.Run in OnSleep to complete when the user closes the app via “Close all”. The app must be designed so that such work can be safely interrupted and resumed later.
References: