flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
11.05k stars 427 forks source link

Android App Crashes on Reopening After Closing with Back Button #2788

Open iforvard opened 7 months ago

iforvard commented 7 months ago

Description

After building an Android APK using Flet, the app crashes when reopened after being closed with the back button. The crash does not occur if the app is closed through the system's "Recent Apps" menu.

Steps to Reproduce:

  1. Build an Android APK using flet build apk
  2. Install the APK on an Android device.
  3. Open the app.
  4. Close the app using the back button.
  5. Open the app again.
  6. The app will crash.

Reproducible on devices:

Samsung S21 Ultra (Android 14) Sberbox (TV stick with Android 9)

The official flet app on google play has the same behavior (video):

https://github.com/flet-dev/flet/assets/48457650/d1ff4f6a-60fe-4bde-81aa-980ecc8e16e6

huangxulei commented 7 months ago

Flut support for Android is not very good, mainly on Windows. My app import some packages, eg. tinytag, which Android could not recognize and show tinytag was not available

yucel685 commented 7 months ago

I'm having the same problem. Is there a way to assign commands to the phone's navigation key?

xkayo32 commented 6 months ago

Estou tendo o mesmo problema. Existe uma maneira de atribuir comandos à tecla de navegação do telefone?

I also want this, instead of closing the application it returns to the page.

dudusandi commented 6 months ago

Same issue with S23

yucel685 commented 6 months ago

I solved this problem with the route change function using views.I used system exit() with import sys for the back key

BrentHuang commented 5 months ago

I'm having the same problem on HUAWEI HarmonyOS.

amun311 commented 5 months ago

add this in your code:

`def event(e): if e.data=='detach' and page.platform == ft.PagePlatform.ANDROID: os._exit(1)

page.on_app_lifecycle_state_change = event`

J-PRS commented 5 months ago

Could it be possible to minimize the app as if middle button was pressed instead of exit, so it will open faster the next time?

amun311 commented 5 months ago

Could it be possible to minimize the app as if middle button was pressed instead of exit, so it will open faster the next time?

Ad this to your code `def event(e): if e.data=='detach' and page.platform == ft.PagePlatform.ANDROID: os._exit(1)

page.on_app_lifecycle_state_change = event`

When you press middle button it will minimize and when you press back button app will close. You need flet 0.4.0 at least

J-PRS commented 5 months ago

I think minimizing would be better but maybe not possible? I've tried before exit on page.on_view_pop and page.on_window_event == minimize, but it didn't work, your solution works perfectly, thank you! It still crashes when opening very fast after closing but hopefully no user will be doing that too much.

MKS313 commented 4 months ago

I solved this problem with the route change function using views.I used system exit() with import sys for the back key

hi, plz help me for closing flet-app when i swipe-back on my android device, i want to render prev view when swipe

Narendra03022007 commented 3 months ago

add this in your code:

`def event(e): if e.data=='detach' and page.platform == ft.PagePlatform.ANDROID: os._exit(1)

page.on_app_lifecycle_state_change = event`

Thank you so much

AbdurRafayWaliKamran commented 2 months ago

Could it be possible to minimize the app as if middle button was pressed instead of exit, so it will open faster the next time?

Ad this to your code `def event(e): if e.data=='detach' and page.platform == ft.PagePlatform.ANDROID: os._exit(1)

page.on_app_lifecycle_state_change = event`

When you press middle button it will minimize and when you press back button app will close. You need flet 0.4.0 at least

Thankssssss it's not crashing now!!!! Now thought now I would have to learn Java or Kotlin! But seems like I can continue with Flet! Thanks again man!

AbdurRafayWaliKamran commented 2 months ago

Bro it didn't completely solve the problem, it still crashes, however the crash has been minimized. I would say, don't frequently open and close your flet program, let it take some time, especially if it is saving some stuff or vice verse (fetching some data). Thanks!

edugoia commented 1 month ago

I also have this problem, and a problem with closing the application, when I try to go back to the previous page with the back button and closing the application, I tried using on_view_pop, but nothing works

kiddik666 commented 2 days ago

My flet 0.24.1 and Android 11 phones have the same problem.