Android Menu and Back Button not working

MJoehlMJoehl USMember ✭✭

Hi

I have an Xamarin.Forms project targeting Android, iOS and UWP.
In my Android-App, the hardware backbuttons of my phone are not working (back button and also the menu button).

Why is that? Did I forgot something?

Thanks

Answers

  • coolrjmcoolrjm INMember ✭✭✭

    What type of application are you creating. Is it a navigation application with multiple pages. Or is it a single page application. By default the hardware buttons are supposed to work as expected unless you write code to disable the functionality.

  • 15mgm1515mgm15 USMember ✭✭✭✭

    The back button should be working if you are using a NavigationPage on your project, the menu button does not do anything till you code something in it's event.

  • MJoehlMJoehl USMember ✭✭

    @15mgm15 said:
    The back button should be working if you are using a NavigationPage on your project, the menu button does not do anything till you code something in it's event.

    You're right... I found out, that the backbutton works as it should. But I detected a really strange behavior:
    When I go to my detailpage I can navigate back with my backbutton. But when I first press the menu-button (I have nothing special declared) then the backbutton also won't work anymore...

  • 15mgm1515mgm15 USMember ✭✭✭✭
    edited May 2016

    @MichaelJhl If you are using a ContentPage you can override this method:

    protected override bool OnBackButtonPressed()
    {
        return base.OnBackButtonPressed();
    }
    

    And then you can debug if this method is being called after you are pressing the Menu button, and if it is not the correct behaviour you can go native and inside your MainActivity of you Android project you can use this method:

    public override void OnBackPressed()
    {
        if (ImInMainPage)
        {
            Finish();
        }
        else
        {
            base.OnBackPressed();
        }
    
    }
    

    Hope this helps.

  • MJoehlMJoehl USMember ✭✭

    @15mgm15 No luck with this... both of the methods are not called when I first pressed the hardware-menu-button. I discovered that the material-theme can be the problem. In a newly created (empty) project without material theme, the menu button (and also the back-button) work as excpected.

    Also strange: In fact, the overflow-menu-button (3-dotted-icon) should only appear, when the device has no hardware-menu-button, but in my case -> it appears everytime.

    Also the Activity-Indicator didn't show up when I set IsBusy to true. But without the Material Theme, it Shows up (no oterh changes).

    Whats happen here? Maybe a bug?

  • 15mgm1515mgm15 USMember ✭✭✭✭

    @MichaelJhl it may be a bug! have you try with different Xamarin.Forms versions and different (previous) AppCompat v7 - v4 nuget packages? Have you tried real devices? Have you tried different emulators and Android version? Maybe this can help you to figure this out.

Sign In or Register to comment.