I have a TabView with 4 tabs (page1, page2, page3, menu). When I click on any tab, onAppear is called, the problem is when I select menu and then click logout to switch from TabView to Login screen, all pages loaded before are called again (onAppear fired again)
I've reproduced with this little example. This is problem for me, cause I have many views where I need to call network service, so I expect that onAppear is called only when switching between tabs. Any solution?
struct ContentView: View {
@State var isLogout = false
var body: some View {
if isLogout {
Text("Login")
} else {
TabView {
Text("Page1")
.onAppear{
print("Page1 Appeared")
}
.tabItem {
Label("Page1", systemImage: "square")
}
Text("Page2")
.onAppear{
print("Page2 Appeared")
}
.tabItem {
Label("Page2", systemImage: "circle")
}
VStack {
Text("Menu")
Button("Logout") {
isLogout = true
}
}
.onAppear{
print("Menu Appeared")
}
.tabItem {
Label("Menu", systemImage: "list.dash")
}
}
}
}
}
Logs:
Page1 Appeared
Page2 Appeared
Menu Appeared
Login Appeared
Page2 Appeared
Page1 Appeared
I don't know why the problem happened. But you can detect when the user switches between tabs by using a variable to store the current selection:
Uses:
Use
onChangedto detect when the user switches tabs:And don't forget to give each view a
tag: