Flet app for Android: Back Navigation using native OS back button is exiting the app
Description I've have 2 routes, initial / route and /store route from /store view, while pressing system back navigation button ....it's triggering disconnect event. how to navigate back to initial / view on pressing it
Code example to reproduce the issue:
#/app/main.py
import flet as ft
async def main(page: ft.Page):
print()
page.theme = ft.Theme(color_scheme_seed= ft.colors.GREEN_500,
page_transitions= ft.PageTransitionsTheme(
android= ft.PageTransitionTheme.CUPERTINO,
linux= ft.PageTransitionTheme.CUPERTINO
))
page.padding = 0
async def route_change(route):
page.views.clear()
page.views.append(
ft.View(
"/",
[
ft.AppBar(title=ft.Text("Flet app"), bgcolor=ft.colors.SURFACE_VARIANT),
ft.ElevatedButton("Visit Store", on_click=gotostore),
],
)
)
if page.route == "/store":
page.views.append(
ft.View(
"/store",
[
ft.AppBar(title=ft.Text("Store"), bgcolor=ft.colors.SURFACE_VARIANT),
ft.ElevatedButton("Go Home", on_click=gohome),
],
)
)
await page.update_async()
async def gohome(e):
await page.go_async('/')
async def gotostore(e):
await page.go_async('/store')
async def view_pop(view):
print('view',view)
page.views.pop()
top_view = page.views[-1]
await page.go_async(top_view.route)
page.on_route_change = route_change
page.on_view_pop = view_pop
await page.go_async('/')
ft.app(target=main)
Describe the results you received:
I've have 2 routes, initial / route and /store route from /store view, while pressing system back navigation button ....it's triggering disconnect event. how to navigate back to initial / view on pressing it
https://github.com/flet-dev/flet/assets/35882740/423b81bc-29c3-45e7-a34b-a5c62508ed42
Describe the results you expected: I expected it to switch back to initial / route from /store route on pressing system back button
Additional information you deem important (e.g. issue happens only occasionally): Please use bottom back navigation button provided by underlying android OS to reproduce the issue. There is no issue while using back button present in App Bar.
Flet version (pip show flet):
Name: flet
Version: 0.20.0.dev2135
Summary: Flet for Python - easily build interactive multi-platform apps in Python
Home-page:
Author: Appveyor Systems Inc.
Author-email: [email protected]
License: Apache-2.0
Location: /home/lekshman/goldfinance/app_env/lib/python3.11/site-packages
Requires: cookiecutter, flet-runtime, packaging, qrcode, watchdog, websocket-client, websockets
Required-by:
Operating system:
command ran on Kali Linux
flet run --android /app
Additional environment details: Tested on Flet Mobile Android Testing App Client Version 1.4.0
Ah, it's the issue inside Flet app for Android. That makes sense as pressing device back button is sending it to "Projects" screen instead of previous route inside the app. I'm afraid not all scenarios could be covered/tested with Flet app. There we have like Flet app inside another Flet app. We could try "redirecting" back button or maybe "block" with https://stackoverflow.com/questions/44635450/is-there-any-way-intercept-back-keydown-in-flutter-app-on-android
The main question is if the app is normally working when built with flet build apk. Have you tried that?
Ah, it's the issue inside Flet app for Android. That makes sense as pressing device back button is sending it to "Projects" screen instead of previous route inside the app. I'm afraid not all scenarios could be covered/tested with Flet app. There we have like Flet app inside another Flet app. We could try "redirecting" back button or maybe "block" with https://stackoverflow.com/questions/44635450/is-there-any-way-intercept-back-keydown-in-flutter-app-on-android
The main question is if the app is normally working when built with
flet build apk. Have you tried that?
Have not built apk but I tested in normal mobile browser and it works. Thanks
But it doesn't work for apk, any solutions? I have tried lots of things but nothing seems to work :(
Android back button event capture and use in routing and exit solved and proved with other people here: https://github.com/flet-dev/flet/discussions/4495#discussioncomment-11634586