flet icon indicating copy to clipboard operation
flet copied to clipboard

back to android in navigation flet

Open SofExp opened this issue 1 year ago • 4 comments

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.

SofExp avatar Dec 10 '24 05:12 SofExp

bro hw did you solve that. Help me please

HappyCoder256 avatar Dec 12 '24 20:12 HappyCoder256

Same problem here, please help

GreenDringo42 avatar Dec 20 '24 16:12 GreenDringo42

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

SIRGPrice avatar Dec 21 '24 09:12 SIRGPrice

any update ?

AttaM1rza avatar Sep 14 '25 19:09 AttaM1rza