AppLifecycleState
States that an application can be in once it is running.
Inherits: enum.Enum
Properties
DETACH- The application has exited, and detached all host views from the engine.HIDE- The application is hidden.INACTIVE- The application loses input focus.PAUSE- The application is paused.RESTART- The application is resumed after being paused.RESUME- The application gains input focus.SHOW- The application is shown.
Examples
App Lifecycle State showcase
import flet as ft
def main(page: ft.Page):
page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
log = ft.Column(
spacing=4,
scroll=ft.ScrollMode.AUTO,
height=220,
)
def add_log(message: str):
log.controls.insert(0, ft.Text(message, size=12))
if len(log.controls) > 20:
log.controls.pop()
log.update()
def on_lifecycle(e: ft.AppLifecycleStateChangeEvent):
add_log(f"Received: {e.state.name}")
page.on_app_lifecycle_state_change = on_lifecycle
page.appbar = ft.AppBar(title="AppLifecycleState Showcase")
page.add(
ft.SafeArea(
expand=True,
content=ft.Column(
controls=[
ft.Text(
"Switch app focus/visibility to see lifecycle state changes."
),
ft.Text(
"Ex: minimize/restore app, switch tabs, or "
"background/foreground app.",
size=11,
),
ft.Row(
wrap=True,
spacing=8,
controls=[
ft.Container(
padding=ft.Padding.symmetric(horizontal=8, vertical=4),
border=ft.Border.all(1, ft.Colors.OUTLINE),
border_radius=12,
content=ft.Text(state.name, size=11),
)
for state in ft.AppLifecycleState
],
),
ft.Container(
width=720,
padding=12,
border=ft.Border.all(1, ft.Colors.RED),
border_radius=10,
bgcolor=ft.Colors.SURFACE_CONTAINER_LOW,
content=log,
),
]
),
)
)
if __name__ == "__main__":
ft.run(main)