flet
flet copied to clipboard
Flet APK exit gracefully
Im sorry for the issue, because I know it should be rather in discussion, but there is near zero activity there... How do I make custom "cleanup" logic right before closing (destroying, removing from memory) the android app?
I have one discussion about that, but seems it stuck (https://github.com/flet-dev/flet/discussions/3555)
The guy from that discussoin advices to use "on_app_lifecycle_state_change" And this is what I came (but "cleanup" logic does not work):
import flet as ft
def main(page: ft.Page):
page.title = "Flet counter example"
page.vertical_alignment = ft.MainAxisAlignment.CENTER
txt_number = ft.TextField(value="0", text_align=ft.TextAlign.RIGHT, width=100)
number = page.client_storage.get("number")
txt_number.value = number if number else "0"
def minus_click(e):
txt_number.value = str(int(txt_number.value) - 1)
page.update()
def plus_click(e):
txt_number.value = str(int(txt_number.value) + 1)
page.update()
row = ft.Row(
[
ft.IconButton(ft.icons.REMOVE, on_click=minus_click),
txt_number,
ft.IconButton(ft.icons.ADD, on_click=plus_click),
],
alignment=ft.MainAxisAlignment.CENTER,
)
page.add(
row,
)
def handler(e:ft.AppLifecycleStateChangeEvent):
if e.data == ft.AppLifecycleState.DETACH:
page.client_storage.set("number", txt_number.value)
page.window.destroy()
page.on_app_lifecycle_state_change = handler
page.window.prevent_close = True
ft.app(main)
So It is a simple counter modifyed so that when you exit (close) app it should save last value, But it doesnt.
The apk compiled successfully, and launches on my phone. All works except that "ft.AppLifecycleState.DETACH" seems never happen.
What Am I doing wrong?