Rg.plugins.popup: OnBackButtonPressed() not fired when hardware back button is pressed whilst on a Popup Page

Created on 7 Mar 2018  路  11Comments  路  Source: rotorgames/Rg.Plugins.Popup

I am in the process of implementing a Popup page in my app, and would like my Popup page to disappear when the hardware back button is pressed.

I am trying to do this by calling the OnBackButtonPressed() function in the code behind for the Popup page but the Popup remains in view until I click on the background, and I noticed whilst debugging that the function is never called.

Is there a way around this?

Most helpful comment

@danibillings

I ran into this as well. Turns out I just hadn't implemented the plugin all the way yet.

If you look at the Getting Started documentation, you'll see the following code at the bottom of the page. This code needs to be pasted into your Android project's MainActivity class. You don't need to do anything inside the if/else statement - the important part is the Rg.Plugins.Popup.Popup.SendBackPressed(base.OnBackPressed)
Once that's in your mainActivity class, the back button works as expected.

public override void OnBackPressed()
        {
            if (Rg.Plugins.Popup.Popup.SendBackPressed(base.OnBackPressed))
            {
                // Do something if there are some pages in the `PopupStack`
            }
            else
            {
                // Do something if there are not any pages in the `PopupStack`
            }
        }

All 11 comments

@danibillings

I ran into this as well. Turns out I just hadn't implemented the plugin all the way yet.

If you look at the Getting Started documentation, you'll see the following code at the bottom of the page. This code needs to be pasted into your Android project's MainActivity class. You don't need to do anything inside the if/else statement - the important part is the Rg.Plugins.Popup.Popup.SendBackPressed(base.OnBackPressed)
Once that's in your mainActivity class, the back button works as expected.

public override void OnBackPressed()
        {
            if (Rg.Plugins.Popup.Popup.SendBackPressed(base.OnBackPressed))
            {
                // Do something if there are some pages in the `PopupStack`
            }
            else
            {
                // Do something if there are not any pages in the `PopupStack`
            }
        }

@Nixon-Joseph

Thank you! I knew I was just missing something small and obvious.

I dont really understand that why should this be in the mainactivity if I am using xamarin forms? is it for xamarin native?

It's because the plugin can't directly access the back button properly, unless android specifically defines how. It is for xamarin.forms, not native.

it is strange because in earlier versions, it was working without this code. i guess this is added for latest version. something should have changed.

Yeah, my guess was it is a future proofing change. But maybe it could have just given the developer more control.

I just tested and it is definetly changed. i was using OnBackButtonPressed() in the popup.xaml.cs. this doesnt work any longer and page behind the popup receives the backbuttonpressed event. if I use this code page behind the popup doesnt receive the backbutton pressed.
I find this not so good as before. I have different popups and i handled each differently. Some pop when back button is pressed, some dont. how to handle that in a centralized class like mainactivity. I must search for name probably. why previous version can handle in each popup page's code behind which is much easier. I cant understand the necessity of this change.
@rotorgames can you please explain why this behavior changed?

ok, just digging into history of changes, it was changed only on 1.1.4.145-pre and reason is described here

https://github.com/rotorgames/Rg.Plugins.Popup/wiki/Migration-from-v1.0.x-to-v1.1.x#android-back-button

That makes sense.

Actually, in popup.xaml.cs, you still need this code, if you want to prevent, backbutton to pop popup. Mainactivity code is only to prevent page behind to receive backbuttonpressed.

protected override bool OnBackButtonPressed() { // Prevent hide popup return true; }

Actually, in popup.xaml.cs, you still need this code, if you want to prevent, backbutton to pop popup. Mainactivity code is only to prevent page behind to receive backbuttonpressed.

  protected   override bool OnBackButtonPressed()
       {
           // Prevent hide popup        
           return true;
       }

Did you got any solution for that?

Was this page helpful?
0 / 5 - 0 ratings