How to switch between windows without opening another in Xcode VisionOS SwiftUI

Here is my button code

Button("Map") {
   openWindow(id: "Navigate")
   dismissWindow(id: "Begin")
}

And here is my main app coding with all the scenes:

var body: some Scene {
        WindowGroup(id: "Begin") {
            MainMenuView()
        }
        
        WindowGroup(id: "Navigate") {
            MapView()
        }

        ImmersiveSpace(id: "ImmersiveSpace") {
            ImmersiveView()
        }.immersionStyle(selection: .constant(.full), in: .full)
 }

It would not dismiss the begin window when I click on the button meaning the window is not gone and it just opens the navigate window. Can you please update my code to switch between views in the same window and fix the problem please?

One option here is to close it onAppear of MapView instead. For example:

    @Environment(\.dismissWindow) var dismiss
var body: some Scene {
...
        WindowGroup(id: "Navigate") {
            MapView()
                .onAppear {
                    dismiss(id: "Begin")
                }
        }
...
 }

Then, your button would just open the WindowGroup with id Navigate, and then when it appears, the original window will be closed.

How to switch between windows without opening another in Xcode VisionOS SwiftUI
 
 
Q