flet icon indicating copy to clipboard operation
flet copied to clipboard

Flet APK exit gracefully

Open ixenion opened this issue 1 year ago • 8 comments

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?

ixenion avatar Jul 02 '24 20:07 ixenion

AppLifecycleState.DETACH

https://api.flutter.dev/flutter/dart-ui/AppLifecycleState.html

burhansvural avatar Jul 03 '24 01:07 burhansvural

AppLifecycleState.DETACH

https://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?

ixenion avatar Jul 03 '24 09:07 ixenion

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)

burhansvural avatar Jul 03 '24 10:07 burhansvural

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 avatar Jul 03 '24 10:07 ixenion

@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.

burhansvural avatar Jul 03 '24 11:07 burhansvural

@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

ixenion avatar Jul 03 '24 21:07 ixenion

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.

burhansvural avatar Jul 04 '24 02:07 burhansvural

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?

ixenion avatar Jul 04 '24 12:07 ixenion

Devs careless attitude is astonishing. Look https://github.com/flet-dev/flet/issues/3610

image

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

ixenion avatar Jul 07 '24 06:07 ixenion

Sorry for the wrong link. Except from that, let's keep only this issue opened.

ndonkoHenri avatar Jul 07 '24 07:07 ndonkoHenri

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?

ixenion avatar Jul 12 '24 19:07 ixenion

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?...

ixenion avatar Jul 23 '24 08:07 ixenion

Hi @ixenion

burhansvural avatar Jul 23 '24 10:07 burhansvural

Hi @ixenion

Dude lol

There was quote above: "How did you know it worked (DETACH) if the value wasnt saved?"

ixenion avatar Jul 23 '24 17:07 ixenion

Why no one can simply read?.. I have to repeat questions multiple times, Its very very very annoying. Guys c'mon

ixenion avatar Jul 24 '24 09:07 ixenion

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.

ixenion avatar Jul 24 '24 09:07 ixenion

@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

Screenshot_20240726-111805_1

Screenshot_20240726-110841_1

Screenshot_20240726-110708_1

ixenion avatar Jul 26 '24 08:07 ixenion

It's been half a year since the issue was made... Any suggestions on how to grasefully exit android app?

ixenion avatar Jan 09 '25 10:01 ixenion

Anybody alive?..

ixenion avatar Jan 25 '25 10:01 ixenion

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.

FeodorFitsner avatar Jan 25 '25 21:01 FeodorFitsner

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?

ixenion avatar Jul 04 '25 11:07 ixenion

Thanks yall. Really appreciate the participation and engagement.

ixenion avatar Sep 21 '25 10:09 ixenion