back to android in navigation flet
Discussed in https://github.com/flet-dev/flet/discussions/4495
Originally posted by SofExp December 5, 2024
Question
I am facing an issue with handling the Android "Back" button in navigation using Flet. The behavior doesn't seem to work as expected. When I press the Back button. Android "Back" button minimizes the application and does not participate in routing. How to detect and intercept the back button in android?
I build the application using flet build apk.
Code sample
import flet
from flet import Page, View, Text, ElevatedButton
def main(page: Page):
def route_change(route):
page.views.clear()
if page.route == "/":
page.views.append(
View(
"/",
controls=[
Text("Home"),
ElevatedButton("page 1", on_click=lambda _: page.go("/screen1")),
ElevatedButton("page 2", on_click=lambda _: page.go("/screen2")),
],
)
)
elif page.route == "/screen1":
page.views.append(
View(
"/screen1",
controls=[
Text("page 1"),
ElevatedButton("Back", on_click=lambda _: page.go("/")),
],
)
)
elif page.route == "/screen2":
page.views.append(
View(
"/screen2",
controls=[
Text("page 2"),
ElevatedButton("Back", on_click=lambda _: page.go("/")),
],
)
)
page.update()
def view_pop(view):
page.views.pop()
page.go(page.views[-1].route if page.views else "/")
page.on_route_change = route_change
page.on_view_pop = view_pop
page.go(page.route)
flet.app(target=main)
Error message
No response
------------------------------------------------------
- [x] I have searched for answers to my question both in the issues and in previous discussions.
bro hw did you solve that. Help me please
Same problem here, please help
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
any update ?