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
When overriding it in the AppShell
it does fire. It doesn't seem to be propagated to underlying pages.
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.
@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.
@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.
Sorry to hear about that @MSiccDev !
Let me know as soon as you have found some time
@jfversluis I tried it as soon as XF 4.2 is out still facing same issue onbackbutton is not executing.
@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
@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.
A quick fix on this is much appreciated because this is stopping me from tasting shell :-(
Sorry to hear about that @MSiccDev !
Let me know as soon as you have found some time
will do
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.
@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.
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 ?
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?
@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"); });
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;
}
}
}
Is this issue getting any love / attention? (yes, I can apply a hack but that's not my style)
@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!
The problem with this issue is that the workaround I need for #8581 is using exactly that :(
@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.
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.
@RaelsonCraftz, thank you your workaround. It works well.
@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?
@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?
Well this is embarrassing
Most helpful comment
A quick workaround I've found is to add this snippet to the AppShell.cs of your project
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.