[XF] Add ability to Remove pages from the navigation stack
A common scenario in mobile app is having the ability to remove a page that exists somewhere in the navigation stack. This is not a navigation operation, but really a targeted way of removing a very specific page.
Example: Initially navigate the app: MasterDetailPage/NavigationPage/ProjectsListPage Navigate to create a new project: MasterDetailPage/NavigationPage/ProjectsListPage/CreateProjectPage when finished with the create page navigate to a session page, and remove the create page: MasterDetailPage/NavigationPage/ProjectsListPage/SessionsListPage
There are two options to achieve this:
- Remove(string name) - you provide the registered name of the page to be removed. This can be very tricky to implement and error prone since there are multiple navigation stacks to look through to find the page. Especially when you have more than one instance of the same page in the navigation stack.
- Remove(int index) - you provide the index of the page to remove. This is safer, but places the responsibility of knowing exactly which page needs to be removed on the developer. Since there are multiple navigation stacks, the Remove method may have to have an optional parameter to tell the service which stack to look in to remove the page.
New Ideas from conversation below:
- IDestroyAfterNavigation - since the main scenario for removing a page is normally to remove the page that is prior to the last page in the stack, we can add an interface which will indicate that the page is to be removed immediately after navigation to the new navigation target has completed.
- Support
NavigateAsync("../Name")- stick with a uri based approach and add support for navigating backwards while appending a view.
Thoughts?
Just to be sure, is there any scenario that a direct remove will be necessary? ( leaving out the marginal uses ) Aren't we actually looking for a scenario that we want forward navigation and have the ability to remove the 'previous' page? If so, can't we indicate this with the Navigate method with a bool or something? ( not sure the navigation service knows what the previous is in the stack - but I would assume it does )
@Depechie I have not yet seen a scenario where you were deep in a navigation stack and wanted to suddenly remove a page in the very start of the stack.. It has always been the page prior to the last navigation action.
Another example is a Login. As user logs in, the app navigates to another page, and now the login page should be removed. Right now, an absolute URI is perfect for login, but if the login happens somewhere else in the app, and not at app startup, then this remove would be helpful.
What do you think the API would look like. Add a bool "removePreviousPage" argument to the NavigateAsync signature?
NavigateAsync(string uri, bool useModalNaviagtion, bool animated, bool removePreviousPage)
I am hesitant to add another signature to the navigate method. I would almost prefer an interface over a change in signature. Something like IDestroyAfterNavigation which simply implements IDestructible, but is used as a marker to remove the page when the target page has been navigated to.
I like the interface approach for the same reason you are mentioning, not touching the current signature. Although it would make discoverability of this new feature not easy, so there is also something positive about adding an extra overload for the NavigateAsync method.
But adding a Remove method ( like you first suggested ), sound not ok for me :)
Now wait what others have to say...
The use cases I've has are always creating for a termporary page that behaves similar to a popup dialogue.
An overload for NavigateAsync would fall down when the temporary page itself is the one performing the navigation. Consider navigating to your Login page, but then pressing a 'Navigate back' button, rather than a 'Cancel' button. The handler for 'Back' would need to know about temporary pages in order to call the overload. You are going to end up having to implement an interface.
I like the interface solution. It wouldn't the first feature that is a little hard to find but a good solid read of the docs always worked for me. IRegionMemberLifetime is very similar to what you might want.
Just thinking out loud here...
NavigateAsync(string uri, bool useModalNaviagtion, bool animated, bool removePreviousPage)
I would say, is probably the wrong way to go about accomplishing what we'd like to see.
Remove(string name) & Remove(int index) I believe both present a design challenge for the NavigationService in it's current state, as it would need to maintain some concept of the navigation stack (which has been asked for periodically for a while). Also for Remove(string name) we would need to establish what the logic would be for something like MainPage/ViewA/ViewB/ViewA which instance of ViewA do you pop?
I do however believe with relative ease we could maintain Uri based navigation with the desired effect. We certainly see this all the time with web design NavigateAsync("../ViewB")
@dansiegel that's an interesting approach. I'm wondering if this would cause confusion between GoBack() or not. Would we also support something like "../../../ViewB"? I can see where that can start to get very difficult to manage. Defintiely worth something worth looking into.
@brianlagunas I would say that anytime the Navigation Service encounters a segment "..", then it would pop a page. As terrible as it would be technically "../ViewA/../ViewB" should be ~~supported~~ allowable....
@dansiegel the difficulty lies in knowing if to PopModalAsync or PopAsync. This would provide a little extra flexibility though. It would probably remove a lot of cases where you have to reset the navigation stack with an absolute URI and rebuild everything.
To define it's function; every "../" encountered would represent a Pop, and then if a view is provided "../ViewName" the view would be created and added to the stack?
@dansiegel also, how would one control if the pop would be modal or just async?
Edit: would we use the existing useModalNavigation parameter? If so, the pops would be one or the other and we will not be able to control them in more complex scenarios.
Perhaps we do need to add a Navigation stack that could help us for the crazy scenarios. We might add something like the following code, where INavigationStack is a singleton registered with the Container.
public interface INavigationStack
{
List<NavigationStackPage> NavigationStack { get; }
}
public class NavigationStackPage
{
public string Name { get; set; }
public bool IsModal { get; set; }
public Type Type { get; set; }
}
If the main scenario is to remove only the last page, we might implement it similar to Prism UWP? https://github.com/PrismLibrary/Prism/blob/master/Source/Windows10/Prism.Windows/Navigation/FrameNavigationService.cs#L125
I've used it before to create custom navigation flows and wizards (so the backstack stays clean). Example on this blog post.
why couldn't these be properties of the NavigationParameters. It seems like we are going to far with adding new interfaces or adding parameters to methods. Wouldn't there be value in NavigateAsyn("ViewA?isModal=true&isAnimated&removePreviousPage=true") and then just adding the explict propeties to NavigationParameters?
@powerdude this is something we are also looking at. We actually are going to make a breaking change in #922 to support this. Though, we cannot add any properties to the NavigationParameetrs class, rather they would have to be extension methods. NavigationParameetrs will be shared across multiple platforms, and some known parameters will not be used on other platforms.
So I have something working with the "../" syntax, but there is a side effect that may not be desirable. Lets say the navigation stack looks like this:
"ViewA/ViewB/ViewC"
Now I navigate using:
NavigateAsync("../../ViewD");
There will be a quick flash of ViewA and then ViewD will be shown. This is because when the previous views are removed, ViewA is made the current page and then the navigation to ViewD occurs. I'm not sure this is acceptable or not.
Also, should the INavigationAware methods be called? I am leaning towards no, since we are not trying to navigate to or from them, but rather remove them completely. I can see calling IDestructible on them
Okay, so I have some good news and bad news.
Good news, I have a working prototype of using "../NewView" to remove pages from the navigation stack without having the flash issue I mentioned above.
Bad news, this can only be supported for NavigationPages and will not work for anything modal. This is because the XF INavigation.RemovePage method is only supported for NavigationPages.
Is this acceptable, or is supporting modal pages also required?
My general thinking is that "../NewView" shouldn't care if the ".." is a modal, wrapped in a NavigationPage, etc. If we're adding RemovePage it would be nice but I would expect something like having a stack "NavigationPage/PageA/PageB/PageC" RemovePage("PageB")
Unfortunately, the only way to achieve support for both modal and non-modal scenarios is to pop the pages off the stack and then add the new pages onto the stack. This causes a quick visual flash of the previous page, which is undesirable.
XF does not have a method that can remove pages off of a modal stack. They must be popped.
So it's either support both and have this weird flash, or only support NavigationPages and have no flash.
It would be better not to have the flash. You can work around not using modal pages in the remove pages scenarios.
could this be something that we could introduce an interface to control which is more important? I see how a number of developers would agree with @snowppy but I would say there are also a number of developers and apps that would require the ability to pop Modal's even given the page flashing (at least until we can work with Xamarin to find a better alternative).
public interface INavigationServiceOptions
{
bool PreferNavigationPageRemoval { get; }
}
I'm sure we could get a better name...