title: Prevent SwiftUI from resetting state of a navigation stack
body: <p>For some unknown reason. Upon reaching the <strong>4th child of the stack</strong>, the Binding attached that displays the 3rd child <strong>gets reset</strong>. The issue here, is <strong>you cannot go back anymore.</strong></p>
  <p>This problem carries over on the 5th child - thus resetting the 4th.</p>
  <p>I even tried to inject an environment object from the root hoping to mitigate the issue but it is still the same.</p>
  <p>Please try to replicate using the code below.</p>
  <pre><code>class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  
    var window: UIWindow?
  
    var globalEnvironment = GlobalEnviroment()
  
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
      let contentView = ContentView().environmentObject(globalEnvironment)
  
      if let windowScene = scene as? UIWindowScene {
          let window = UIWindow(windowScene: windowScene)
          window.rootViewController = UIHostingController(rootView: contentView)
          self.window = window
          window.makeKeyAndVisible()
      }
    }
  
    // other callbacks
  }
  </code></pre>
  <p>Then for the views, I added logging so its easy to see the issue</p>
  <pre><code>import SwiftUI
  
  class GlobalEnviroment: ObservableObject {
    @Published var isPage2Active = false {
      didSet {
        print(&quot; [2] something set this to \(isPage2Active)&quot;)
      }
    }
    @Published var isPage3Active = false {
      didSet {
        print(&quot; [3] something set this to \(isPage3Active)&quot;)
      }
    }
    @Published var isPage4Active = false {
      didSet {
        print(&quot; [4] something set this to \(isPage4Active)&quot;)
      }
    }
    @Published var isPage5Active = false {
      didSet {
        print(&quot; [5] something set this to \(isPage5Active)&quot;)
      }
    }
  }
  
  struct ContentView: View {
    @State private var isDetailActive = false
    @EnvironmentObject var env: GlobalEnviroment
  
    var body: some View {
      print(&quot; [root] redrawing...&quot;)
      return NavigationView {
        VStack {
          NavigationLink(destination: Page2(isActive: $env.isPage2Active)
                          .environmentObject(env),
                         isActive: $env.isPage2Active) {
            EmptyView()
          }
          Button(action: {
            env.isPage2Active = true
          }, label: {
            Text(&quot;Next&quot;)
          })
        }
      }
      .onReceive([env.isPage2Active].publisher.first(), perform: { response in
        print(&quot; 2: \(response)&quot;)
      })
      .onReceive([env.isPage3Active].publisher.first(), perform: { response in
        print(&quot; 3: \(response)&quot;)
      })
      .onReceive([env.isPage4Active].publisher.first(), perform: { response in
        print(&quot; 4: \(response)&quot;)
      })
      .onReceive([env.isPage5Active].publisher.first(), perform: { response in
        print(&quot; 5: \(response)&quot;)
      })
      .onAppear {
        print(&quot; ============================================  &gt;&gt;&gt;&gt; &quot;)
        print(&quot; [root] appeared&quot;)
      }
      .onDisappear {
        print(&quot; [root] disappear&quot;)
        print(&quot; &lt;&lt;&lt;&lt;  ============================================ &quot;)
      }
    }
  }
  
  struct Page2: View {
    @Binding var isActive: Bool
    @State private var isDetailActive = false
    @EnvironmentObject var env: GlobalEnviroment
  
    var body: some View {
      print(&quot; [2] redrawing...&quot;)
      return VStack {
        Text(&quot;Page 2&quot;)
            .padding()
        NavigationLink(destination: Page3(isActive: $env.isPage3Active)
                        .environmentObject(env),
                       isActive: $env.isPage3Active) {
          EmptyView()
        }.isDetailLink(true)
        Button(action: {
          env.isPage2Active = false
        }, label: {
          Text(&quot;Back&quot;)
        })
          .padding()
        Button(action: {
          env.isPage3Active = true
        }, label: {
          Text(&quot;Next&quot;)
        })
      }
      .navigationBarBackButtonHidden(true)
      .onAppear {
        print(&quot; ============================================  &gt;&gt;&gt;&gt; &quot;)
        print(&quot; [2] appeared&quot;)
      }
      .onDisappear {
        print(&quot; [2] disappear&quot;)
        print(&quot; &lt;&lt;&lt;&lt;  ============================================ &quot;)
      }
    }
  }
  
  struct Page3: View {
    @Binding var isActive: Bool
    @EnvironmentObject var env: GlobalEnviroment
  
    var body: some View {
      print(&quot; [3] redrawing...&quot;)
      return VStack {
        Text(&quot;Page 3&quot;)
            .padding()
        NavigationLink(destination: Page4(isActive: $env.isPage4Active)
                        .environmentObject(env),
                       isActive: $env.isPage4Active) {
          EmptyView()
        }.isDetailLink(true)
        Button(action: {
          print(&quot;ikaw??&quot;)
          env.isPage3Active = false
        }, label: {
          Text(&quot;Back&quot;)
        })
          .padding()
        Button(action: {
          env.isPage4Active = true
        }, label: {
          Text(&quot;Next&quot;)
        })
      }
      .navigationBarBackButtonHidden(true)
      .onAppear {
        print(&quot; ============================================  &gt;&gt;&gt;&gt; &quot;)
        print(&quot; [3] appeared&quot;)
      }
      .onDisappear {
        print(&quot; [3] disappear&quot;)
        print(&quot; &lt;&lt;&lt;&lt;  ============================================ &quot;)
      }
    }
  }
  
  struct Page4: View {
    @Binding var isActive: Bool
    @State private var isDetailActive = false
    @EnvironmentObject var env: GlobalEnviroment
  
    var body: some View {
      print(&quot; [4] redrawing...&quot;)
      return VStack {
        Text(&quot;Page 4&quot;)
            .padding()
        NavigationLink(destination: Page5(isActive: $env.isPage5Active)
                        .environmentObject(env),
                       isActive: $env.isPage5Active) {
          EmptyView()
        }.isDetailLink(true)
        Button(action: {
          env.isPage4Active = false
        }, label: {
          Text(&quot;Back&quot;)
        })
          .padding()
        Button(action: {
          env.isPage5Active = true
        }, label: {
          Text(&quot;Next&quot;)
        })
      }
      .navigationBarBackButtonHidden(true)
      .onAppear {
        print(&quot; ============================================  &gt;&gt;&gt;&gt; &quot;)
        print(&quot; [4] appeared&quot;)
      }
      .onDisappear {
        print(&quot; [4] disappear&quot;)
        print(&quot; &lt;&lt;&lt;&lt;  ============================================ &quot;)
      }
    }
  }
  
  struct Page5: View {
    @Binding var isActive: Bool
    @EnvironmentObject var env: GlobalEnviroment
  
    var body: some View {
      print(&quot; [5] redrawing...&quot;)
      return VStack {
        Spacer()
        Button(action: {
          env.isPage5Active = false
        }, label: {
          Text(&quot;Back&quot;)
        })
          .padding()
        Text(&quot;Page 5&quot;)
            .padding()
        Spacer()
      }
      .navigationBarBackButtonHidden(true)
      .onAppear {
        print(&quot; ============================================  &gt;&gt;&gt;&gt; &quot;)
        print(&quot; [5] appeared&quot;)
      }
      .onDisappear {
        print(&quot; [5] disappear&quot;)
        print(&quot; &lt;&lt;&lt;&lt;  ============================================ &quot;)
      }
    }
  }
  
  </code></pre>