Share via

MAUI app exiting without crashing after inactivity

Jai Holloway 110 Reputation points
2026-05-25T04:10:52.8133333+00:00

Hi there

I have a .NET 10 MAUI app and it is exiting on refresh, or any activity, after being in "sleep" mode for a while. The shortest time I have tried is 4 hours, but I believe 2 hours will have it exit too. I have tried leaving the simulator running to see what happens, but the simulator exits without crashing too. How do I prevent a stale app from going stale and exiting?

Developer technologies | .NET | .NET Multi-platform App UI
0 comments No comments

2 answers

Sort by: Most helpful
  1. Nancy Vo (WICLOUD CORPORATION) 5,700 Reputation points Microsoft External Staff Moderator
    2026-05-25T06:12:15+00:00

    Hello @Jai Holloway ,

    Thanks for your question.

    This is most likely the OS silently killing your app to free memory.

    I recommend some following workarounds:

    1. Save and restore state
    protected override void OnSleep()
    {
        Preferences.Set("last_page", "YourPageName");
    }
    
    protected override void OnResume()
    {
        var lastPage = Preferences.Get("last_page", "MainPage");
    }
    
    protected override void OnStart()
    {
        var hadSession = Preferences.Get("last_page", null);
        if (hadSession != null)
        {
            // restore session
        }
    }
    
    1. If your app uses authentication, please refresh token on resume.
    protected override async void OnResume()
    {
        var tokenExpiry = Preferences.Get("token_expiry", DateTime.MinValue.ToString());
        var expiry = DateTime.Parse(tokenExpiry);
    
        if (DateTime.UtcNow >= expiry)
        {
            await RefreshTokenAsync();
        }
    }
    
    1. Simulator is not reliable for inactivity testing. It's better to test on a real device.

    4.If your app needs to stay alive in the background (iOS only), declare background modes in Info.plist.

    <key>UIBackgroundModes</key>
    <array>
        <string>fetch</string>
        <string>remote-notification</string>
    </array>
    

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

    Was this answer helpful?


  2. AI answer

    2026-05-25T04:11:09.7633333+00:00

    Mobile operating systems routinely terminate apps that have been inactive or stopped for a long period, even if they did not crash. In .NET MAUI this is expected behavior when the app is in the stopped state for a while: the OS may reclaim resources and destroy the process. There is no supported way to force the OS to keep a “stale” app alive indefinitely.

    In .NET MAUI, when the window is no longer visible the Stopped event is raised, and there is explicitly “no guarantee that an app will resume from this state, because it may be terminated by the operating system.” When the OS later destroys the app, the Destroying event is raised and the app returns to the not running state. If the user comes back after that, the app is started again from a cold start.

    To handle this correctly:

    1. Use lifecycle events to save and restore state:
      • Handle Stopped to disconnect from long‑running processes and persist any necessary state.
      • Handle Resumed to re‑subscribe to events and refresh visible content when the app returns from a stopped state.
      • Handle Destroying to clean up resources and detach event handlers.
    2. Implement these by deriving from Window and overriding CreateWindow in App to return the custom window, then wiring up the Created, Activated, Deactivated, Stopped, Resumed, and Destroying events.
    3. If using Application.OnSleep/OnResume patterns, ensure important state is saved when the app goes into the background and restored when it comes back, so that a full restart after long inactivity still provides a seamless experience.

    This approach does not prevent the OS from terminating the app after long inactivity, but ensures that when the app is restarted or resumed, it behaves correctly and does not appear to “exit” unexpectedly from the user’s perspective.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

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.