简体   繁体   中英

Xamarin Android

In my Xamarin project I am using masterDetailPage and replacing the detail section with a new ContentPage. I think Android's hardware back button is have the problem knowing you are on a different page. I tried adding base.OnBackPressed in the MainActivity but that didn't fix it. When using the app on Android and Navigate to a new page the back button should send you back to the original page the current app will minimize the program.
Is there anyway to do this?

var masterDetailPage = Application.Current.MainPage as MasterDetailPage;
masterDetailPage.Detail = new BaseNavigationPage((new SearchPage()));

MainActivity

public override void OnBackPressed()
{
    base.OnBackPressed();
}

2 things :

  • In android OnBackPressed() , return false if it is master detail page: this will stop application from closing.

  • Have your own stack for keeping track of all the navigations on MasterDetailPage

Stack<ContentPage> myStack = new Stack<ContentPage> ();

var masterDetailPage = Application.Current.MainPage as MasterDetailPage;
var naviPage = new BaseNavigationPage ((new SearchPage ()));
masterDetailPage.Detail = naviPage;
myStack.Push (naviPage);

//in Master detail page
public override void OnBackPressed () {
    if (myStack.Count > 1) {
        var lastPageLoaded = myStack.Pop ();
        this.Detail = new BaseNavigationPage (lastPageLoaded);
    }
}

I wouldnt suggest making the app always go back to previously loaded page, supppose if the user opens same detail page like 100 times then clicking back would be same page. If thats required push only of the current page is different than last page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM