I am developing a cross platform application using Xamarin.Forms.
I have two pages page1 and page2.
When moving from page2 to page1, using Navigation.PopAsync, I need to show an alert dialog to let the user confirm the action. Only after the user confirms, I need to pop the page.
I need to trap the actions of both the 'Home' button as well as the 'Back' button in android.
How can this be done ?
Answers
Actually that is pretty simple for the Back-Button. But I think there is no way to track the Home-Button.
For the Back-Button you have to override
OnBackButtonPressed
. Here is how I do it:On Page2 make a bool value.
But , is it possible to trap the OnOptionsItemSelected event inside a page renderer ?
We can get the Activity instance using Context of the page inside the renderer. But using that activity instance, can we define the OnOptionsItemSelected method ?
I'm not sure I understand what you want.
Could you show some code of what you're meaning?
Inside a custom PageRenderer, I can get the current Activity using
Activity activity = Context as Activity;
Is there a way, in which I can use this activity instance to define the
OnOptionsItemSelected(IMenuItem item)
method of the Activity class ?Uhm that should work yes. But you need OptionItems then. The call is
activity.OnOptionsItemSelected
Navigation.PopAsync
, you can find more information about Popups here@ahmadmadi : My motive is to trap the back and home button press. So I have no option of deliberately calling Navigation.PopAsync. It is popped internally on back and home press.
@RaphaelSchindler : the
activity.OnOptionsItemSelected
is just a public virtual method and there is no event-delegate mechanism for that , as of now.@SampathKumar If it's virtual then override it like this
@RaphaelSchindler : Inside a Page renderer, I believe we cant override it, though we can get the activity instance.
Ah yes you're right. Haven't tested it in a
PageRenderer
, just inMainActivity.cs
. Maybe you can achive what you wan't with the defined stuff inApp.cs
I finally got a way around to show alert on pressing home..
I had only one Activity in my application. So inside my Activity.cs,
`public override bool OnOptionsItemSelected (IMenuItem item)
{
if (Android.Resource.Id.Home != item.ItemId)
return base.OnOptionsItemSelected (item);
Then inside, the OnBackButtonPressed of my 'Page' I had a check as you mentioned, to show an alert.
Previously I was believing that the 'OnBackButtonPressed' callback for a page precedes the OnBackPressed of the activity.
But it seems , it is the other way around.
OnBackButtonPressed is called only after the OnBackPressed of the activity.
This is the reason why OnBackButtonPressed is not called when we tap the home button in a page.
This works for me..
protected override bool OnBackButtonPressed() { Device.BeginInvokeOnMainThread(async() => { var result = await this.DisplayAlert("Alert!", "Do you really want to exit?", "Yes", "No"); if (result) await this.Navigation.PopAsync(); // or anything else }); return true; }
@ManojkumarMali
How can i apply Device.BeginInvokeOnMainThread(async() => {
var result = await this.DisplayAlert("Alert!", "Do you really want to exit?", "Yes", "No");
if (result) await this.Navigation.PopAsync(); // or anything else
});
in " OnDisappearing()" method ?
@Eswaran
You can make a use of
OnBackButtonPressed
event.There is no use of Disappearing event, because it called when page disappears.
@ManojkumarMali
thanks for your reply.
OnBackButtonPressed event is called only when we click the hardware back button is clicked. Actually I need to display a alert message when i click the Navigation bar back button(i.e. software back button). For Navigation bar back button the OnBackButtonPressed event is not called instead OnDisappearing event is called where i cant able to perform async function.
@ManojkumarMali
Thanks, It works for me.
You saved my Day.
Has anyone figured out how to intercept the navigation back button (not the hardware back button) being tapped on the navigation bar so that one can ask the user if they really want to exit/pop the page?
Navigation offers the Popped event to hook into, which is great for view/viewmodel cleanup, but I believe it's too late to be asking at that point whether they really want to close the page. Ideas?
What I do in cases like that is push the page modally, and add a Cancel toolbar item. You can get confirmation before popping the modal page in both
OnBackButtonPressed
and the toolbar item click handler, and there will be no back button in the navigation bar.