Reputation:
I am using Xamairn.Forms, How I can Disable Back Button In Navigation Bar when
ViewList
Is Refreshing.
Actually, I am using the following code to disable Default Back Button, but right now I am trying to find away also to disable Navigation Bar back button
protected override bool OnBackButtonPressed()
{
if (ListView.IsRefreshing)
return true;
return false;
}
Any Idea how I Can Disable Back Button In Navigation bar?
Upvotes: 2
Views: 18673
Reputation: 1
For Android/Xamarin the following is the best/quickest solution
Just Use :
NavigationPage.HasBackButton="False"
https://stackoverflow.com/a/59464036/3245472
Upvotes: 0
Reputation: 171
Just do that. For me it works for Android
protected override bool OnBackButtonPressed(){
return true;
}
Upvotes: 1
Reputation: 482
You can use the following code to disable the back button press in Xamarin Forms:
protected override bool OnBackButtonPressed()
{
return false;
}
I hope it will help you. Warning: This event is not raised on iOS. here
Upvotes: 0
Reputation: 1
You can use this simple code to prevent user to navigate back NOTE that you should have only one page (root page) that the user can navigate back to it. for example we have page named "Login", "Home" and "Cars" pages and the root page is Login
=> inside Home page:
Home home= this;
Navigation.PushAsync(new Cars());
Navigation.RemovePage(home);
=> you should do this to the rest of the pages except root page!
Upvotes: 0
Reputation: 11105
Overall Android and iOS do not have normal APIs to prevent a navigation bar back button from being executed for one very good reason, the user expects to be able to go back and you should not prevent them from doing so.
You should instead change your design by only giving the user the options you want them to be able to make or hide the navigation bar entirely and create your own bar with your own buttons.
Adam's guide provides a great over view of the subject which I wish I would have found when faced with this issue.
He suggest the same things I just did but also provides VERY hacky ways to prevent navigating back if you refuse to change your design (code below is lifted mostly unchanged from the article linked above):
On Android you can attempt to find the back button and use the ContentPage
's ViewModel method to find out if going back should be allowed or not. You would overwrite OnOptionsItemSelected
within MainActivity
:
public override bool OnOptionsItemSelected(IMenuItem item) {
var app = Application.Current;
if (item.ItemId == 16908332) { // This makes me feel dirty.
var navPage = ((app.MainPage.Navigation.ModalStack[0] as MasterDetailPage).Detail as NavigationPage); // Notice this code assumes it is looking for a MasterDetailPage being shown as a modal
if (app != null && navPage.Navigation.NavigationStack.Count > 0) {
int index = navPage.Navigation.NavigationStack.Count - 1;
var currentPage = navPage.Navigation.NavigationStack[index];
var vm = currentPage.BindingContext as ViewModel; //You would want to cast this to a base ViewModel or a specific one so that OnBackButtonPressed() can be called
if (vm != null) {
var answer = vm.OnBackButtonPressed();
if (answer) {
return true;
}
}
}
}
return base.OnOptionsItemSelected(item);
}
On iOS you need a custom renderer to switch out the real back button for your own custom one:
[assembly: ExportRenderer(typeof(Page), typeof(CustomPageRenderer))]
namespace Mobile.iOS.CustomRenderer {
public class CustomPageRenderer : PageRenderer {
public override void ViewWillAppear(bool animated) {
base.ViewWillAppear(animated);
var page = Element as CorePage;
if (page != null) {
if ((page).OverrideBackButton) {
var root = this.NavigationController.TopViewController;
// NOTE: this doesn't look exactly right, you need to create an image to replicate the back arrow properly
root.NavigationItem.SetLeftBarButtonItem(new UIBarButtonItem("< Back", UIBarButtonItemStyle.Plain, (sender, args) => {
var navPage = page.Parent as NavigationPage;
var vm = page.BindingContext as ViewModel;
if (vm != null) {
var answer = vm.OnBackButtonPressed();
if (!answer)
navPage.PopAsync();
} else {
navPage.PopAsync();
}
}), true);
}
}
}
}
}
On Windows Phone... just override OnBackButtonPressed
... working with WP is actually easy for once.
Upvotes: 3