Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug]Shell - OnBackButtonPressed not executing #7072

Closed
Anil-R opened this issue Aug 6, 2019 · 25 comments
Closed

[Bug]Shell - OnBackButtonPressed not executing #7072

Anil-R opened this issue Aug 6, 2019 · 25 comments
Labels
a/backbutton a/shell 🐚 e/2 🕑 2 in-progress This issue has an associated pull request that may resolve it! t/bug 🐛

Comments

@Anil-R
Copy link

Anil-R commented Aug 6, 2019

Description
When navigating back to previous page using hardware back button OnBackButtonPressed is not fired or executed.

Steps to Reproduce
Navigate to a page.
Click hardware back button

Expected Behavior
OnBackButtonPressed should be called

Actual Behavior
OnBackButtonPressed is not called

Basic Information
Version with issue: 4.1
Platform Target Frameworks:
Android: 9
Affected Devices: All Android Devices and Emulators

@Anil-R Anil-R added s/unverified New report that has yet to be verified t/bug 🐛 labels Aug 6, 2019
@pauldipietro pauldipietro added this to New in Triage Aug 6, 2019
@jfversluis
Copy link
Member

When overriding it in the AppShell it does fire. It doesn't seem to be propagated to underlying pages.

@jfversluis jfversluis added e/2 🕑 2 and removed s/unverified New report that has yet to be verified labels Aug 6, 2019
@jfversluis jfversluis moved this from New to Ready For Work in Triage Aug 6, 2019
@samhouts samhouts added this to To do in Other Ready For Work Aug 13, 2019
@samhouts samhouts removed this from Ready For Work in Triage Aug 13, 2019
@MSiccDev
Copy link

MSiccDev commented Sep 1, 2019

I have the same problem with two XF apps. Thanks @jfversluis for the override hint. At least I am able to implement a workaround now. One show-stopper less.

@jfversluis
Copy link
Member

@MSiccDev could you maybe try to see if it still doesn't work in 4.2?

Trying to reproduce this again with the latest code and I seem I am unable to.

@MSiccDev
Copy link

MSiccDev commented Sep 2, 2019

@jfversluis will do once it does not break the major parts of my apps. I have been hit with several bugs with 4.2 (non pre) and Shell on Android.

@jfversluis
Copy link
Member

Sorry to hear about that @MSiccDev !

Let me know as soon as you have found some time

@Anil-R
Copy link
Author

Anil-R commented Sep 2, 2019

@jfversluis I tried it as soon as XF 4.2 is out still facing same issue onbackbutton is not executing.

@PureWeen
Copy link
Contributor

PureWeen commented Sep 2, 2019

@XamDev-Anil not to say this shouldn't work but can you try using the BackButtonBehavior?

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/navigation#back-button-behavior

It's a bit more integrated with Shell and we made a lot of upgrades to it with 4.2

@Anil-R
Copy link
Author

Anil-R commented Sep 2, 2019

@PureWeen Thanks for the suggestion , Most of my Application users are Android. Android users use hardware back button at most of times . So need to ensure some stuff are in right order before they go back and stop if not.

@Anil-R
Copy link
Author

Anil-R commented Sep 2, 2019

A quick fix on this is much appreciated because this is stopping me from tasting shell :-(

@MSiccDev
Copy link

MSiccDev commented Sep 5, 2019

Sorry to hear about that @MSiccDev !

Let me know as soon as you have found some time

will do

@mjfreelancing
Copy link

I am also experiencing this problem with Shell. I have a modal page that does fire OnBackButtonPressed() but it isn't called on any of my other pages.

On a side note, I cannot see any option to hide the back button - I still want the nav bar so the title is displayed. Is it possible to also hide the button?

Hope this incorrect behaviour is fixed soon.

@IgorKravchenko10
Copy link

@XamDev-Anil not to say this shouldn't work but can you try using the BackButtonBehavior?

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/navigation#back-button-behavior

It's a bit more integrated with Shell and we made a lot of upgrades to it with 4.2

Shell BackButtonBehavior is good but in OnBackButtonPressed method we can return true which will not return us to previous page. It will be good to have bool property like ToReturn in this behavior.

@qz2rg4
Copy link

qz2rg4 commented Oct 30, 2019

Hi All, I am experiencing the same problem on 4.3 OnBackButtonPressed does not get called but OnBackPressed gets called in the MainActivity

I am calling the page as Shell.Current.GoToAsync("Test");

The navigation back button works perfectly it is only the hardware back button which does not work.

Has anyone found a solution to this problem ?

@Dintwe
Copy link

Dintwe commented Nov 14, 2019

Hello All, I am experiencing the same issue on XF 4.3.0 Shell, OnBackButtonPressed() does not fire for Android hardware back button, any workaround or fix? the client is breathing down my neck, I have to stop the app going back when the popup modal is displayed, what can I do?

@qz2rg4
Copy link

qz2rg4 commented Nov 15, 2019

@Dintwe I know this is not ideal but my workaround was by using messaging center from the main activity

  public override void OnBackPressed()
        {
            var OnBackPressed = "Backwards"; MessagingCenter.Send(OnBackPressed, "Backwards");
}

Then subscribing to it in the form
MessagingCenter.Subscribe<string>(this, "Backwards", (sender) => { DisplayAlert("BackButton", "BackButtonPressed" + sender, "OK"); });

@Dintwe
Copy link

Dintwe commented Nov 15, 2019

Thank you @qz2rg4 for the prompt response I really appreciate it, thank you very much.

One workaround I concocted is to have a flag accessible through singleton.

namespace NS.Global
{
    public class GlobalSettings
    {
        public static GlobalSettings Instance { get; } = new GlobalSettings();
        public bool IsPopupOn { get; set }
    }
} 

I can set IsPopupOn anywhere in the app through static Instance.

then access it in AppShell, as @jfversluis pointed out that OnBackButtonPressed() in AppShell gets fired when hardware back button is pressed.

namespace NS
{
    public partial class AppShell : Xamarin.Forms.Shell
    {
        public static String AppName = "AppName";

        public AppShell()
        {
            InitializeComponent();
        }

        protected override bool OnBackButtonPressed()
        {
            if (!Global.GlobalSettings.Instance.IsPopupOn)
                return base.OnBackButtonPressed();

            return true;
        }
    }
}

@mjfreelancing
Copy link

Is this issue getting any love / attention? (yes, I can apply a hack but that's not my style)

@samhouts samhouts added this to Backlog in Shell Jan 3, 2020
@jfversluis
Copy link
Member

@mjfreelancing we will definitely get around to it at some point. If you are dependant on it to be fixed soon for the time being you might want to do with a workaround, or if you're up for it, open a PR to fix it so everyone can benefit from it :)

Thanks!

@jonx
Copy link

jonx commented Jan 6, 2020

The problem with this issue is that the workaround I need for #8581 is using exactly that :(

@mjfreelancing
Copy link

@jfversluis I'd love to open a PR, except I don't know where to start. I did take a cursor look several weeks ago and while I could kind-of see why it wasn't working I haven't got a clue about the most appropriate fix. If I get time to take another look I'll reach out to someone for guidance.

@raelsonraujo
Copy link

raelsonraujo commented May 5, 2020

A quick workaround I've found is to add this snippet to the AppShell.cs of your project

protected override bool OnBackButtonPressed()
{
    var page = (Shell.Current?.CurrentItem?.CurrentItem as IShellSectionController)?.PresentedPage;
    if (page.SendBackButtonPressed())
        return true;
    else
        return base.OnBackButtonPressed();
}

This should wire it back to the button of whatever page is being displayed. Of course, if the back button of that page returns false, it'll proceed as normally. I'll bring a note here if any side effects happen on my app.

@holecekp
Copy link

@RaelsonCraftz, thank you your workaround. It works well.

@samhouts samhouts added the in-progress This issue has an associated pull request that may resolve it! label May 28, 2020
@samhouts samhouts moved this from To do to In progress in Other Ready For Work May 28, 2020
@samhouts samhouts moved this from Backlog to In Progress in Shell May 28, 2020
Other Ready For Work automation moved this from In progress to Done Jun 1, 2020
Shell automation moved this from In Progress to Done Jun 1, 2020
@domagojmedo
Copy link

@PureWeen OnBackButtonPressed is called in latest XF version, but even when returning true it still does the default behaviour (pops the page). That doesn't happen with @RaelsonCraftz workaround.
Am I missing something?

@tomeverin
Copy link
Contributor

@PureWeen I'm having the same experience as @domagojmedo - OnBackButtonPressed is called, however if I return true the event still propagates. I want to handle and consume hardware back button presses. Any ideas?

@PureWeen
Copy link
Contributor

Well this is embarrassing

#11094

@samhouts samhouts moved this from Done to In progress in Other Ready For Work Jun 18, 2020
@samhouts samhouts moved this from Done to In Progress in Shell Jun 18, 2020
@samhouts samhouts added this to In Progress in v4.6.0 Jun 20, 2020
@samhouts samhouts moved this from In Progress to Done in v4.6.0 Jun 20, 2020
samhouts pushed a commit that referenced this issue Jun 25, 2020
* Invert logic for SendBackButtonPressed on Shell

* - additional test
fixes #7072
@samhouts samhouts moved this from In Progress to Done in Shell Jul 13, 2020
@samhouts samhouts removed this from Done in Shell Aug 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/backbutton a/shell 🐚 e/2 🕑 2 in-progress This issue has an associated pull request that may resolve it! t/bug 🐛
Projects
Other Ready For Work
  
In progress
v4.6.0
  
Done
Development

No branches or pull requests