Share via

OVERVIEW AND CLOSEALL

Giorgio Sfiligoi 656 Reputation points
2026-04-23T19:38:21.0366667+00:00

Environment: MAUI 10

In Android, pressing the Overview button calls OnSleep, that I use for some clear-house, such as uploading a file to cloud storage. The following snippet is a simulation:


    protected async override void OnSleep()
    {
        base.OnSleep();
        Debug.WriteLine("OnSleep");
        //(Shell.Current.CurrentPage as SecondMainPage)?.SetActInd(true); NON FUNZIONA
        await Task.Run(async () =>
        {
            await Task.Delay(5000);
            Debug.WriteLine("Completed");
        });
    }

If you immediately press 'Close All' after the Overview button, the system kills the process and the task does not complete.

I would like that Android awaits the task to complete anyway.

Developer technologies | .NET | .NET MAUI

3 answers

Sort by: Most helpful
  1. Q&A Assist
    2026-04-23T19:38:35.4466667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    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 mapped OnPause / OnStop events 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.Stopped event maps to Android OnStop, 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 / OnApplicationCreate or 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:

    1 person found this answer helpful.
  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Nancy Vo (WICLOUD CORPORATION) 3,550 Reputation points Microsoft External Staff Moderator
    2026-04-24T03:57:03.2233333+00:00

    Hello @Giorgio Sfiligoi ,

    Thanks for your question.

    Even though you wrote it as async, the MAUI/Android system fires it and moves on, it does not wait for your task to complete. So the moment you presses "Close All", Android kills the process immediately, and your upload task is cut off with no chance to finish.

    It's simply how Android lifecycle works. OnSleep() was not designed to run long-running tasks.

    I recommend moving your work outside of the app process lifecycle, into an Android Foreground Service. Because a Foreground Service runs independently from your app process. Even if your app is killed, the service keeps running until the job is done.

    While this is a non-Microsoft link, it’s official Android documentation and is safe to visit.

    I hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.