.onChange(of: scenePhase) == .active not firing

Hey everyone,

How do I detect that my View is coming into the foreground? The app has been suspended by iOS before, because I closed it (which is fine).

I am running into the strange problem, that .onChange(of: scenePhase) is never triggered on my custom subview. It only works if I attach it to the first view inside the root view.

Some context: My app is iOS 17 and iPhone only.

does work: on TabView() inside ContentView()

ContentView() is the root view of the app.

struct ContentView: View {

    @Environment(\.scenePhase) var scenePhase

        var body: some View {
        
               TabView {
                    CustomSubView()
               }
               .onChange(of: scenePhase) { oldPhase, newPhase in
            
                   if newPhase == .active {
                   // some code, does work
                            
                   } else if newPhase == .background {
                   // some code, does work
               }
       }
}

does not work: on CustomSubView() inside TabView() inside ContentView()

struct CustomSubView: View {
    
    @Environment(\.scenePhase) var scenePhaseCustomSubView

    var body: some View {

        NavigationStack {

         }
         .onChange(of: scenePhaseCustomSubView) { oldPhase, newPhase in
            
                if newPhase == .active {
                    // some code, is not firing for some reason -> use onAppear?
                }

            
                else if newPhase == .background {
                     // some code, does work
                }
            
            }
        }
 }

Thank you for your help

.onChange(of: scenePhase) == .active not firing
 
 
Q