iOS15 NavigationView lost the state after putting app in background and bring it back

In the app, if I follow the navigations from MainView->Tab1->Link1->Sub Link1, put the app to background, then bring it back, then it shows back to Tab1 again, does anyone know why NavigationView cannot keep the last view? it works fine in iOS 14.7

struct LocalNotificationDemoView: View {
    @StateObject var localNotification = LocalNotification()
    @ObservedObject var notificationCenter: NotificationCenter
    var body: some View {
        NavigationView {
            VStack {
                MainView()
            }
        }
        .navigationViewStyle(.stack)
    }
}

struct MainView: View {
    var body: some View {
        TabView {
            Tab1()
                .tabItem {
                    Text("Tab1")
                }
            
            Tab2()
                .tabItem {
                    Text("Tab2")
                }
        }
    }
}

struct Tab1: View {
    @State var selection: Int? = nil
    var body: some View {
        NavigationLink(destination: View1(), tag: 1, selection: $selection) {
            Text("Link 1")
        }
    }
}

struct Tab2: View {
    @State var selection: Int? = nil
    var body: some View {
        NavigationLink(destination: Text("Link 2").navigationTitle("").navigationBarHidden(true), tag: 1, selection: $selection) {
            Text("Link 2")
                .onAppear {
                    print("Link2 shows")
                    Thread.callStackSymbols.forEach{print($0)}
                }
        }
    }
}

struct View1: View {
    @State var selection: Int? = nil
    var body: some View {
        NavigationLink(destination: Text("Sub Link 1"), tag: 1, selection: $selection) {
            Text("Sub Link 1")
        }
    }
}
Answered by MobileTen in 696043022

Move the parent NavigationView from LocalNotificationDemoView to the MainView tabs. There is no need for the .navigationViewStyle(.stack) modifier.

struct LocalNotificationDemoView: View {
    var body: some View {
        VStack {
            MainView()
        }
    }
}

struct MainView: View {
    var body: some View {
        TabView {
            NavigationView {
                Tab1()
            } .tabItem {
                Text("Tab1")
            }

            NavigationView {
                Tab2()
            }.tabItem {
                Text("Tab2")
            }
        }
    }
}
Accepted Answer

Move the parent NavigationView from LocalNotificationDemoView to the MainView tabs. There is no need for the .navigationViewStyle(.stack) modifier.

struct LocalNotificationDemoView: View {
    var body: some View {
        VStack {
            MainView()
        }
    }
}

struct MainView: View {
    var body: some View {
        TabView {
            NavigationView {
                Tab1()
            } .tabItem {
                Text("Tab1")
            }

            NavigationView {
                Tab2()
            }.tabItem {
                Text("Tab2")
            }
        }
    }
}

That works, thanks! Then the follow up question is how can I hide the tab bar when I follow "Link 1" and subsequent links?

Actually, I end up with running away from TabView and using something like https://www.hackingwithswift.com/forums/swiftui/navigationview-with-tabview-on-chatting-app/40 suggests, customized TabViewBar

I face a similar issue here: https://developer.apple.com/forums/thread/695366. It's not exactly the same, but the behavior is similar enough that it looks like it's the same root issue. Seems like there's something wrong when NavigationView and TabView are used together in SwiftUI IOS 15 :(

iOS15 NavigationView lost the state after putting app in background and bring it back
 
 
Q