Reputation: 1486
So Previously I wanted to make my app to go back to MainPage after the Pyshical BackButton is pressed. I've already done that but the problem is when I am on MainPage and press the Pyhsical Button, instead of closing the App it doesn't go anywhere. The second problem is when the page stack is 2 (MainPage > News > NewsDetail) it goes directly to MainPage (DetailNews > MainPage). What I want to do is to pop to previouse Page (DetailNews > News > MainPage > ). Here is my full code for overide the backbutton
public bool DoBack
{
get
{
MasterDetailPage mainPage = App.Current.MainPage as MasterDetailPage;
if (mainPage != null)
{
bool canDoBack = mainPage.Detail.Navigation.NavigationStack.Count > 1 || mainPage.IsPresented;
// we are on a top level page and the Master menu is NOT showing
if (!canDoBack)
{
// don't exit the app just show Dashboard
//mainPage.IsPresented = true;
Type page = typeof(DrawerPage);
mainPage.Detail = new NavigationPage((Page)Activator.CreateInstance(page));
return false;
}
else
{
return true;
}
}
return true;
}
}
What Code should i Add or change here to do that, Any Sugestion ?
Upvotes: 0
Views: 708
Reputation: 1486
Im Already Found the solution im override OnBackButtonPressed in every Detail Page
protected override bool OnBackButtonPressed()
{
Device.BeginInvokeOnMainThread(async () => {
Application.Current.MainPage = new DrawerPage();
});
return true;
}
Upvotes: 0
Reputation: 804
The first problem: You logic is wrong. You need to return true if you want to stay on MainPage.
public bool DoBack
{
get
{
MasterDetailPage mainPage = App.Current.MainPage as MasterDetailPage;
if (mainPage != null)
{
bool canDoBack = mainPage.Detail.Navigation.NavigationStack.Count > 1 || mainPage.IsPresented;
// we are on a top level page and the Master menu is NOT showing
if (!canDoBack)
{
// don't exit the app just show Dashboard
//mainPage.IsPresented = true;
Type page = typeof(DrawerPage);
mainPage.Detail = new NavigationPage((Page)Activator.CreateInstance(page));
return true;
}
else
{
return false;
}
}
return true;
}
}
The second problem: The reason why it goes directly to MainPage from DetailsNews is because you call PopToRoot instead of Pop. Read this article.
Upvotes: 1