Reputation: 631
I have a MasterDetailPage and the detail page is the navigation page. I want PopAsync when detail page stack is > 1. When it's = 1, the application needs ask if the user wants. Currently, it's working only when it has 2 pages in stack(root and second one), if you are with 3 pages it pops async all the pages and goes to root page. Also if you are at the root page already, it doesn't asks, simply close the app.
PS: The confirmation dialog is working properly in other parts of app.
public async override void OnBackPressed()
{
if (Rg.Plugins.Popup.Popup.SendBackPressed(base.OnBackPressed))
await App.Current.MainPage.Navigation.PopPopupAsync();
else
{
if (App.Current.MainPage is MasterDetailPage mdp)
{
if (mdp.Detail.Navigation.NavigationStack.Count > 1)
await mdp.Detail.Navigation.PopAsync();
else
{
Alerta alerta = new Alerta();
bool opt = await alerta.ShowAlert("confirm", "App name", "Não existem páginas para retornar, você já está na página inicial.", "Continuar", "Encerrar");
if (!opt)
Finish();
}
}
else
{
Alerta alerta = new Alerta();
bool opt = await alerta.ShowAlert("confirm", "App name", "Não existem páginas para retornar, você já está na página inicial.", "Continuar", "Encerrar");
if (!opt)
Finish();
}
}
}
Upvotes: 1
Views: 119
Reputation: 588
In PCL
protected override bool OnBackButtonPressed()
{
base.OnBackButtonPressed();
// Custom logic for BackButtonPresssed
Device.BeginInvokeOnMainThread(async () =>
{
var result = await DisplayAlert("Warning", "Are you sure you want to exit the application?", "Yes", "No");
if (result)
{
MessagingCenter.Send<HomePage>(this, "Shutdown");
}
});
return true;
}
In Android MainActivity OnCreate()
Add Below Code
MessagingCenter.Subscribe<HomePage>(this, "Shutdown", (sender) =>
{
OnShutdown();
});
Upvotes: 1