State loss and sheets dismiss on backgrounding app

I've been hitting a weird SwiftUI bug with navigation and state loss and I've managed to reproduce in a very tiny sample project. I've submitted a Feedback FB21681608 but thought it was worth posting here incase any SwiftUI experts can see something obviously wrong.

The bug

With deeper levels of navigation hierarchy SwiftUI will dismiss views when backgrounding the app.

Any work around would be appreciated. This happens in a real app where we have to navigate to a settings screen modally and then a complex flow with other sheets.

Sample code

Happens in the simulator and on device.

import SwiftUI

struct ContentView: View {
    @State private var isPresented = false
    
    var body: some View {
        Button("Show first sheet") {
            isPresented = true
        }
        .sheet(isPresented: $isPresented) {
            SheetView(count: 1)
        }
    }
}

struct SheetView: View {
    private enum Path: Hashable {
        case somePath
    }
    @State private var isPresented = false
    var count: Int
    
    var body: some View {
        NavigationStack {
            VStack {
                Text("Sheet \(count)")
                    .font(.largeTitle)
                // To recreate bug show more than 4 sheets and then switch to the app switcher (CTRL-CMD-Shift-H).
                // All sheets after number 3 dismiss.
                Button("Show sheet: \(count + 1)") {
                    isPresented = true
                }
            }
            .sheet(isPresented: $isPresented) {
                SheetView(count: count + 1)
            }
            // Comment out the `navigationDestination` below and the sheets don't dismiss when app goes to the background.
            // Or move this modifier above the .sheet modifier and the sheets don't dismiss.
            .navigationDestination(for: Path.self) { _ in
                fatalError()
            }
        }
    }
}

Thanks for the post, this is very interesting. And thanks for the bug where includes the focused project sample.

Navigating complex hierarchies in SwiftUI while maintaining state can indeed be challenging. I can try to suggest some potential workarounds and strategies to mitigate state loss issues during navigation and app backgrounding but I also want to see this issue and use your focus sample to see how the sheets are lost after being presented on the app and to see if moving state management to a parent view is the answer and understand how the views are not being shown after the background. Passing state down through view hierarchies can ensure it's retained longer than if it were localized in deeply nested views.

If you're not already on the latest SwiftUI and iOS version, consider updating to see if the issue has been addressed in more recent releases. Additionally, beta versions can sometimes provide pre-release fixes.

You can see the status of your feedback in Feedback Assistant. There, you can track if the report is still being investigated, has a potential identifiable fix, or has been resolved in another way. The status appears beside the label "Resolution." We're unable to share any updates on specific reports on the forums.

For more details on when you'll see updates to your report, please see What to expect after submission.

Albert Pascual
  Worldwide Developer Relations.

State loss and sheets dismiss on backgrounding app
 
 
Q