Cleanup data before switching screen #3747
-
Is there anyway to clear all data before I switch to other screen class Home(Screen):
data = []
def compose(self) -> None:
self.data = fetch_some_data()
yield ScrollableContainer(*self.data, id="container")
yield Button("Go to Info", id="info-btn")
def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "info-btn":
self.app.switch_screen("info")
class InfoScreen(Screen):
def compose(self) -> None:
yield Static("Info Screen")
yield Button("Back", id="back-btn)
def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "back-btn":
self.app.switch_screen("home") For some reason, when I return to HomeScreen I can see that |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It's a little tricky to help here as it's not clear what all if your code is doing, but I would question the suggestion that from textual.app import App, ComposeResult
from textual.screen import Screen
from textual.widgets import Label
class Info(Screen):
BINDINGS = [
("space", "switch_screen('home')"),
]
def compose(self) -> ComposeResult:
self.app.bell()
yield Label("This is the info screen.")
class Home(Screen):
BINDINGS = [
("space", "switch_screen('info')"),
]
def compose(self) -> ComposeResult:
self.app.bell()
yield Label("This is the home screen.")
class SwitchScreenApp(App[None]):
SCREENS = {
"home": Home(),
"info": Info(),
}
def on_mount(self) -> None:
self.push_screen("home")
if __name__ == "__main__":
SwitchScreenApp().run() If If you want to do something when a screen is switched, you probably want to be looking at the |
Beta Was this translation helpful? Give feedback.
It's a little tricky to help here as it's not clear what all if your code is doing, but I would question the suggestion that
fetch_some_data
"runs again". If you're usingswitch_screen
to switch between screens, you're going to be switching between existing instances, and socompose
will only be called once for each screen. You can confirm this with this little example: