Expected Behavior of Back Button on Navigation Page
Hello,
Ive been working with your newest navigation sample for a couple of days now. Great job! It has helped reduce complexity in our project by a considerable amount. Thank You.
Question: if I have a navigation flow such as the following MyMasterDetail/MyNavigationPage/ViewA
what is the difference in using the back button on the navigation page and the go back method on the PRISM Navigation Service to move from ViewA to MyNavigationPage. Do I need to handle the button press? Are the same events fired? Will they both remove the page from the stack?
Thank You, Bill
Calling NavigationService.GoBack and hitting the back button do the exact same thing. The only difference is, for the moment, the INavigationAware methods don't get called when using the back button. I hope to fix that soon.
By the way, you don't go from ViewA to the NavigationPage. Your ViewA is the root of the NavigationPage, so if you call GoBack from ViewA, nothing will happen. Now, if you call GoBack from MyNavgationPage then you will exit the app because you just popped modal the MasterDetail.
yes that is what brought up the question. just wanted to make sure I wasn't somehow on a different navigation stack. thank you for that clarification.
Hello,
what behavior should I expect here?
if I have a navigation flow such as the following MyMasterDetail/MyNavigationPage/ViewA
Then from ViewA I navigate to ViewB by setting the use modal flag to true resulting in this flow MyMasterDetail/MyNavigationPage/ViewA/ViewB
Then I navigate back to ViewA by using the GoBack() on the nav service ending in this flow MyMasterDetail/MyNavigationPage/ViewA/
Question: Should I expect the onNavigatedTo event to fire on ViewA when ViewB calls GoBack()?
the scenario is essentially a user using a master detail page to navigate to a customer list which is hosted in a nav page. from there they navigate to a specific customer "ViewA" and from there a subset of information on the specific user "ViewB"
If the answer is YES was this a recent commit?
Thank You, Bill
This is correct. Since ViewA was the view you navigated to by calling GoBack, then INavigationAware is called on ViewA. It wouldn't make sense to call INavigationAware methods on any other view that was not the target of the navigation.
No, this is how it should always behave.
the response time on this forum is incredible. thank you for that.
on the topic of navigation is there an option available to pass an instance of a page? as an example if ViewB in the above scenario has very expensive resources with potentially lengthy initialization for reasons beyond my control (3rd party api) that I would prefer to reuse rather than create/dispose. can I reuse a page instance like is possible with XF.Navigation
There will never be an option to pass a page type via navigation, but in a future version there will be a way to cache pages for use during navigation.
"but in a future version there will be a way to cache pages for use during navigation." this would be fantastic thank you,
Earlier you said: "This is correct. Since ViewA was the view you navigated to by calling GoBack, then INavigationAware is called on ViewA. It wouldn't make sense to call INavigationAware methods on any other view that was not the target of the navigation."
I am not observing this behavior. (last build I grabbed from GIT was about 10 days ago) in the code below in App.cs I navigate to the UserContactListView and its OnNavigatedToEvent is fired as expected. When navigating to UserContactRequestListView from UserContactListView and calling GoBack() to return to UserContactListView its OnNavigatedTo is not fired which I did expect to happen based on your response.
here is the complete process. I'm guessing I either have something wrong or misinterpreted your response
CALLED IN APP.CS await NavigationService.Navigate("RootMasterDetailView/MainNavigationView/UserContactListView");
CALLED IN USERCONTACTLISTVIEWMODEL.CS _navigationService.Navigate("UserContactRequestList", useModalNavigation:true);
CALLED IN USERCONTACTREQUESTLISTVIEWMODEL.CS _navigationService.GoBack();
Hmmm... I'll test that out on as soon as I can. There could be a bug there with the GoBack. The logic to determine how to find the previous page in the stack may be flawed. I have a busy day tomorrow so I won't be able to look at this until Sunday.
Since you are running from source, if you have time to debug through the GoBack logic that would be very helpful. If you find the issue, you can also submit a fix :)
no problem. ill let you know what i find or that i fixed my own issue :-)
I was unable to reproduce this using the sample XF solution. the implementation is straight forward enough. for whatever reason my solution has poppedPage as null. I have no idea why but this doesn't appear to be an issue with PRISM
var poppedPage = await DoPop(page.Navigation, useModalForDoPop, animated);
if (poppedPage != null)
OnNavigatedTo(previousPage, segmentParameters);
I'm wondering if I'm possibly using the Navigation Service in a manner that is not valid. Is the following process correct?
this is from App.cs and its the first time the Nav service is invoked. the intent is to load the apps main master detail view a navigation view and a content page for the navigation view await NavigationService.Navigate("RootMasterDetailView/MainNavigationView/DashboardView");
Now if the user opens the menu page for the master detail view (rootMasterDetailView) and clicks a menu item to navigate to their contact list is it valid to do the following (leave off 'RootMasterDetailView/' from the uri) with the intent on NOT having to reload to master detail view. this is being called from the MainNavigationViewModel await NavigationService.Navigate("MainNavigationView/UserContactListView");
and then from there if the user wants to see a detail view of a contact within the contact list they click a list item and they navigate to the UserProfileDetailView using this Navigation call originating from the above targets view model (UserContactListViewModel) await NavigationService.Navigate("UserProfileDetailView");
didn't spend a lot of time with it but on a first pass of the above implementation it does appear to allow the masterdetailview to not have to be reloaded and all works as expected aside from not having the menu icon for the masterdetailview that is used to open the menu
Remember, navigation is always relative to where you are calling Navigate().
If you open the menu from MasterDetailPage to navigate, then you must call navigate from the MasterDetailPageViewModel in order to replace the Detail with the target view.
Calling Navigate() from your MainNavigationViewModel will not be replacing the Detail of the MasterDetailPage, but rather navigating another page onto the MainNavigationView navigation stack. Also, if you are calling NavigationService.Navigate("MainNavigationView/UserContactListView"); from your MainNavigationViewModel, then you are actually adding another NavigationPage that navigates to your UserContactListView to your stack. Remember, your target is relative to where you are calling Navigate.
So, if you want to push another page onto your MainNavigationView, then you simply call Navigate("TheOtherView"); from your MainNavigationViewModel.
Hi Brian, hopefully they give you a day off now and then!
Ok my problem was clearly not understanding the mechanics of the navigation service. your reply makes perfect sense. Thank you very much for the continued help
After spending some time reviewing your response I realized I had initially misinterpreted it. I wasn't clear on exactly what you meant by navigating from MainNavigationView. You wrote : "So, if you want to push another page onto your MainNavigationView, then you simply call Navigate("TheOtherView"); from your MainNavigationViewModel."
Based on the sample solution I am assuming you meant if I wanted to push an additional page onto the stack I would simply call navigate from the content pages viewmodel (one hosted in the navigation page) and not on MainNavigationViewModel which in my solution is the same as MyNavigationPageViewModel in the XF sample. In the sample no navigation is executed from that page only in the master detail and the actual content pages that are loaded into the navigation page.
which means I still have a problem with onNavigatedTo not being called when closing a modal. I am able to reproduce the issue with the sample slightly modified
In the sample XF solution App.cs calls NavigationService.Navigate("MyMasterDetail/MyNavigationPage/ViewA");
change ViewAViewModel Nav method to: _navigationService.Navigate("ViewB", useModalNavigation:true);
change ViewBViewModel Nav method to: _navigationService.GoBack(useModalNavigation: true);
Navigate to ViewB then back to ViewA and ViewAViewModel's onNavigatedToEvent does not fire. the reason is the method below returns null for the previous page. the issue appears to be here
--------snippet from method below----- MODALSTACKCOUNT EVALUTES TO 1 AS EXPECTED int modalStackCount = page.Navigation.ModalStack.Count;
THIS LINE WILL RESULT IN PREVIOUSPAGEINDEX BEING ASSIGNED A VALUE OF -1 (modal stack count of 1 - (-2)) int previousPageIndex = modalStackCount - 2;
THIS LINE WILL ALWAYS EVALUATE TO FALSE DUE TO PREVIOUSPAGEINDEX being >0 if (modalStackCount > 0 && previousPageIndex >= 0)
-------snippet from method below------
FROM PageNavigationService.cs static Page GetPreviousPage(Page page, bool useModalNavigation) { Page previousPage = null;
if (useModalNavigation)
{
int modalStackCount = page.Navigation.ModalStack.Count;
int previousPageIndex = modalStackCount - 2;
if (modalStackCount > 0 && previousPageIndex >= 0)
{
previousPage = page.Navigation.ModalStack[previousPageIndex];
}
}
else
{
int navStackCount = page.Navigation.NavigationStack.Count;
int previousPageIndex = navStackCount - 2;
if (navStackCount > 0 && previousPageIndex >= 0)
{
previousPage = page.Navigation.NavigationStack[previousPageIndex];
}
if (previousPage == null)
previousPage = GetPreviousPage(page, true);
}
return previousPage;
}
That is probably happening because you are going from a modal stack to a non modal. I'll have to research that.
I do find it odd that you are navigating modal from a NavigationPage. Not sure that makes sense from a mobile navigation perspective.
you wrote: "I do find it odd that you are navigating modal from a NavigationPage"
I didn't think I was but its also very possible I have some concepts mixed up. Its to my understanding that in XF if I want to load a page into the detail property of a master detail page and I want to be able to navigate to other pages from that page then it must be a navigation page.
so in my scenario I load the master detail , the navigation page and a content page RootMasterDetailView/MainNavigationView/UserContactListView
once I'm on UserContactListView there are navigation options that use both true/false for modal. if the user is looking at their contact list and they have pending contact requests a link is present. this is just a one page view that has a list of requests and an accept/decline button. it doesn't navigate to any other page so I use modal = true. and GoBack(modal = true) to return from it
from the same UserContactListView the user may select a contact from the list. in this scenario I use modal = false because the page they are going to UserProfileDetailView has additional navigation options so I use non modal navigation. from the UserProfileDetailView the user may elect to see a map view, a street view or a list of notification options all accessed via modal screens.
In general I looked at modal like I would a flyout or popup control and non modal for actual pages. if this is not correct please let me know.
I still wouldn't be surprised if I'm just not getting something but in the GoBackMethod() the result of the call to GetPreviousPage() is passed to OnNavigatedTo(previousPage, segmentParameters);
wouldn't we want to pass in the page that is the target of the navigation and not the page that was just popped off the modal stack?
taking a closer look at GetPreviousPage() on the PageNavigationService and I see how it works flawlessly for non modal navigation. However when using modal navigation it seems that we cant actually get a handle on the previous page regardless of the usemodal flag. When entering the method after invoking GoBack() from a modal neither the modal stack nor the nav stack contains the actual last page. The only page that appears to be accessible is the last modal that was opened. It would seem that we would need access to the page that called Navigate(modal = true) because that is the page instance that needs to be passed to OnNavigatedTo method of PageNavigationClass for its OnNavigatedToEvent to fire.
out of curiosity why do we set the modal stack count variable to -2? To me that is implying that we think both the content page and the modal page are in the same stack but I really haven't spent much time looking at it. I know that XF treats both as individual navigation stacks. It almost seems like we are working with a different Navigation context in this method because I am unable to see the page that invoked the modal.
static Page GetPreviousPage(Page page, bool useModalNavigation) { Page previousPage = null;
if (useModalNavigation)
{
int modalStackCount = page.Navigation.ModalStack.Count;
int previousPageIndex = modalStackCount - 2;
if (modalStackCount > 0 && previousPageIndex >= 0)
{
previousPage = page.Navigation.ModalStack[1];
}
}
else
{
int navStackCount = page.Navigation.NavigationStack.Count;
int previousPageIndex = navStackCount - 2;
if (navStackCount > 0 && previousPageIndex >= 0)
{
previousPage = page.Navigation.NavigationStack[previousPageIndex];
}
if (previousPage == null)
previousPage = GetPreviousPage(page, true);
}
return previousPage;
}
the following code would appear to address the issue with not passing the correct 'previous page'. not sure its the most elegant but it does get the events to fire as expected
if we could add this to the GoBack() method as the function that gets the previous page that is passed to OnNavigatedTo
the intent here is to get a reference back to the viewmodel that called navigate(modal = true) so that we can fire its on navigatedto event when the modal is closed
var page = GetCurrentPage();
PrismApplicationBase MyPRISMApp = (PrismApplicationBase)page.Parent;
MasterDetailPage MyDetailPage = (MasterDetailPage)MyPRISMApp.MainPage;
NavigationPage MyNavigationPage = (NavigationPage)MyDetailPage.Detail;
var canNavigate = await CanNavigateAsync(page, segmentParameters);
OnNavigatedTo(MyNavigationPage.CurrentPage, segmentParameters);
The suggested code is very specific to this particular navigation stack and will not work for every other possible navigation scenario. I will have to think about the problem more in depth while considering all navigation scenarios and see of I can think of a solution.
agreed. I have been thinking about other scenarios that may be impacted. not sure if its a simple overload/flag for this one use case or if the permutations would get out of hand. Considering MasterDetail page is one of the most common navigation patterns I think it probably should be supported somehow especially if the nav service allows for it from a navigation standpoint.
but this problem I don't feel is specific to masterdetail if I'm not mistaken its going to surface any time someone uses modal navigation and then calls GoBack(). If your on ViewA and you invoke the navigation service with modal = true to get to ViewB when you call GoBack from ViewModelB OnNavigatedTo on ViewModelA will not fire because the incorrect 'previous' page gets passed to OnNavigatedTo in the PageService class. combine this with Xamarin not having hooks into the back button on the Nav page so we can also fire OnNavigatedTo on that click event results in having to implement a number of workarounds or rethink navigation. PRISM offers the same nav concepts across all clients that's one of the reasons I love it!
if I come across any other use cases for GoBack ill share them
thanks again