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?
AppLifecycleState.DETACH
https://api.flutter.dev/flutter/dart-ui/AppLifecycleState.html
AppLifecycleState.DETACHhttps://api.flutter.dev/flutter/dart-ui/AppLifecycleState.html
Will there be any comments on why you showed me exactly same enum which I used (and it didnt work) And on the link?
ft.AppLifecycleState.DETACH only running on IOS and Android
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):
print(e.state)
# ft.AppLifecycleState.DETACH only running on IOS and Android
if e.state == 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)
ft.AppLifecycleState.DETACH only running on IOS and Android
It would be nice if you read my question again from the start to the end carefully, And not skipping any words))
I wrote that "The APK compiled successfully, and launches on my phone. All works except that "ft.AppLifecycleState.DETACH" seems never happen."
I thought it would be clear that if I use words "apk" and "phone" that means Im already from the beginning testing it on android device.
@ixenion I'm sorry I didn't see it.
But the last codes I wrote work without any problems on my Android 12 device. I can only see DETACH working. So the event is triggered, there are deficiencies in the other codes coming from behind, I couldn't deal with them separately.
@ixenion I'm sorry I didn't see it.
But the last codes I wrote work without any problems on my Android 12 device. I can only see DETACH working. So the event is triggered, there are deficiencies in the other codes coming from behind, I couldn't deal with them separately.
Did you managed to run it successfully? I mean, when you exit (completely close, forse stop, whatever) the app remembers last value on next startup?
"I can only see DETACH working" For me is opposite - I can see all except "DETACH".
What is your flet and flutter version? Could you provide link to the apk youv built?
Because I tryed your code, and got same results as mine. It works, but it doesnt save the value on exit
Yes, it does not save the value at exit. I said this before because there is something missing in its codes. The issue I was focusing on was whether the DETACH event was triggered or not.
Yes, it does not save the value at exit. I said this before because there is something missing in its codes. The issue I was focusing on was whether the DETACH event was triggered or not.
How did you know it worked (DETACH) if the value wasnt saved?
Devs careless attitude is astonishing. Look https://github.com/flet-dev/flet/issues/3610
He even linked wrong issue. 3566 is not mine and reports about absolutely different thing.
Im afraid with such attitude my "question will be addressed" not very soon
Sorry for the wrong link. Except from that, let's keep only this issue opened.
Sorry for the wrong link. Except from that, let's keep only this issue opened.
Why it can take so long? Im not asking to add featire to the lib, The question is very simple: Is it possible or not?
Yes, it does not save the value at exit. I said this before because there is something missing in its codes. The issue I was focusing on was whether the DETACH event was triggered or not.
Hello?...
Hi @ixenion
Hi @ixenion
Dude lol
There was quote above: "How did you know it worked (DETACH) if the value wasnt saved?"
Why no one can simply read?.. I have to repeat questions multiple times, Its very very very annoying. Guys c'mon
Why no one can simply read?.. I have to repeat questions multiple times, Its very very very annoying. Guys c'mon
@ndonkoHenri Thats why Im opening exactly same new issues.
@FeodorFitsner
Please, could you check this issue from the top? Seems you are in charge of "AppLifecycleState" class, And for some reason AppLifecycle.State.DETACH not working properly
It's been half a year since the issue was made... Any suggestions on how to grasefully exit android app?
Anybody alive?..
We will look into this issue when its priority increases, if more people are interested in the solution. Adding a lot of comments to the issue won't increase its priority, I'm sorry.
We will look into this issue when its priority increases, if more people are interested in the solution. Adding a lot of comments to the issue won't increase its priority, I'm sorry.
Hi Feodor! Do you still think that this is not what people need?
Thanks yall. Really appreciate the participation and engagement.