Pop up page not closing when click device back button

SreeeeSreeee INMember ✭✭✭✭✭

I am using Rg.Plugins.Popup NuGet package for showing pop up page. When clicking the android device back button the pop-up page is not hiding, it only closes the content pages. I am working on a .Net standard project.

Versions:

Rg.Plugins.Popup - 1.1.5.180

Xamarin Forms - 3.3.0.967583

Pop-up Page Codes

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

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

BackgroundClicked closing the pop-up page, but the device back button pressed not closing the pop-up page.

Best Answer

  • SreeeeSreeee IN ✭✭✭✭✭
    Accepted Answer

    For the Android back button to work with the plugin, we should invoke Rg.Plugins.Popup.Popup.SendBackPressed in your MainActivity in override method OnBackPressed

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        ...
    
        public override void OnBackPressed()
        {
            if (Rg.Plugins.Popup.Popup.SendBackPressed(base.OnBackPressed))
            {
                // Do something if there are some pages in the `PopupStack`
                await PopupNavigation.Instance.PopAsync();
            }
            else
            {
                // Do something if there are not any pages in the `PopupStack`
            }
        }
    
      ....
    }
    

    Got this solution from here.

Answers

  • SreeeeSreeee INMember ✭✭✭✭✭
    Accepted Answer

    For the Android back button to work with the plugin, we should invoke Rg.Plugins.Popup.Popup.SendBackPressed in your MainActivity in override method OnBackPressed

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        ...
    
        public override void OnBackPressed()
        {
            if (Rg.Plugins.Popup.Popup.SendBackPressed(base.OnBackPressed))
            {
                // Do something if there are some pages in the `PopupStack`
                await PopupNavigation.Instance.PopAsync();
            }
            else
            {
                // Do something if there are not any pages in the `PopupStack`
            }
        }
    
      ....
    }
    

    Got this solution from here.

Sign In or Register to comment.