There is a pretty classic scenario when the view was shown and you send view shown event to some analytics platform.
How do I detect if the first view appeared after the second one was closed?
var body: some View {
Button("Show second") {
isSecondViewShown.toggle()
}
.onAppear {
print("First appeared")
}
.fullScreenCover(isPresented: $isSecondViewShown) {
Button("Close second") {
isSecondViewShown.toggle()
}
.onAppear {
print("Second appeared")
}
}
}
onAppear
(which feels natural) does not work in this case. When you tap "Show Second" button and then "Close Second" button the following will be printed in logs:
First appeared
Second appeared
Instead of:
First appeared
Second appeared
First appeared
You can of course observe isSecondViewShown
but it's not reliable and misleading.