PushAsync Can't Push PageTwice

Mostly, when a page is navigated to, we want to create a new page and use the PushAsync() method to navigate to it. However, in some cases, we will want to keep a Page stored as a variable and repush the Page on to the stack. However, I tried this today and it doesn't work. I don't get an error, but nothing happens.

How can I push a page on to the navigation stack that has already been on the stack before?

Answers

  • Sahadev_GuptaSahadev_Gupta INMember ✭✭✭

    @MelbourneDeveloper Can you post your code.

  • PierreParmPierreParm FRMember ✭✭
    edited March 2017

    Returns

    An awaitable Task, indicating the PopModalAsync completion. The Task.Result is the Page that has been popped.

    Are you storing the result and storing this or trying to push the same page twice ?
    Maybe you should try to await the result and then push the page ?

  • JohnHardmanJohnHardman GBUniversity mod
    edited March 2017

    @MelbourneDeveloper - I tried re-using pages about 2 years ago, but took it out again as there were some issues doing it on certain platforms (I think it was Windows platforms, but am not 100% sure at this point). Although the pages were displayed when pushed onto the navigation stack after having previously popped them from the navigation stack, there were issues around laying out the page content of a very few pages (most worked, but a couple didn't). I have seen posts quite recently where other people said they had this working, so I have just tried it again. All of the pages I have tried today seem to be ok (I suspect something relevant has changed in XF since I last tried this, although it could be a result of me re-factoring my own code), with the exception of toolbars which don't appear the second time I push the same page (this is probably due to cleanup that I do in a base page class when pages are popped - I haven't checked this yet).

    Can you post your relevant code, both for the page that you are re-using, and the code for pushing it?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    We do what you're describing in our app.

    The thing to remember is that your WidgetPage can't be both the 4th element in the stack and the 9th element in the stack. So you can't just blindly push a page over and over, relying on the built-in system to handle it all for you.

    We wrote code to handle our navigation... look at the queue... and if a page already existing on the navigation queue then we 'unwind` to it (back, back, back {...} )

    We eventually changed from pushing pages to having a single page and swapping out content in the middle: that way we kept a fixed header and footer on the page. Of course that required writing our own navigation system. The advantage there is that we can traverse forward and backward as we like.

    Its like anything in life: You get what you pay for. If you use the built-in system (free) you don't get as custom of a solution as if you invest in making your own.

  • JohnHardmanJohnHardman GBUniversity mod

    @MelbourneDeveloper - Like @ClintStLaurent , I have developed my own navigation code. On a phone it is simply a layer over what Xamarin provide (including the bit that Clint mentioned about looking at the queue, and then finding the best way to traverse to the desired page). On a tablet or desktop, my code switches to display two "pages" side-by-side, basically doing this by having one master page that I then insert the content from the two pages into (which is similar to what Clint describes).

    Regarding what you are asking about (re-using cached page objects) - whilst I had removed that due to problems a couple of years ago, from my tests earlier today, it looks like it now works. My gut feel is that it might be found to be fragile as updated Xamarin.Forms NuGet packages are released, so I'd be tempted to make it easily switchable so you can flick back and forth between using caching of page objects or not caching page objects. Even then, you might only want to do it on pages that are used a lot, or at least use weak references to the page objects.

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭
    edited March 2017

    Thanks @ClintStLaurent and @JohnHardman

    I like this idea a lot:

    We wrote code to handle our navigation... look at the queue... and if a page already existing on the navigation queue then we 'unwind` to it (back, back, back {...} )>

    I will see if this is a valid workaround.

    @ClintStLaurent, do you know if there is a way to see the stack of pages? Isn't there a collection maintained somewhere? I couldn't find it. Do I have to manually maintain a stack myself?

    @JohnHardman said:

    @MelbourneDeveloper - I tried re-using pages about 2 years ago, but took it out again as there were some issues doing it on certain platforms (I think it was Windows platforms, but am not 100% sure at this point).

    I am testing on UWP, so perhaps it is just a problem in UWP. I will try other platforms to make sure the behaviour is not different. It might be simply a bug in in Xamarin Forms.

  • KyleWarrenKyleWarren USUniversity ✭✭
    Are you asking if you nav to A, nav to B, nav to C, then back to B... that the end nav stack look like

    A/B/C/B

    or

    A/B @MelbourneDeveloper
  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭

    I'm not sure if this is a question or not. But, if this is a question, then the answer is no.

  • KyleWarrenKyleWarren USUniversity ✭✭
    edited March 2017
    What is the scenario you're asking for?
  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭

    As I've mentioned in the opening post, I need to be able to Push a Page in to the navigation stack more than once. Some people here have recommended some work-arounds and I will be testing those when I have a chance.

  • KyleWarrenKyleWarren USUniversity ✭✭
    I'm wondering if when you push the second page if you are expecting the stack to be reset or not?

    You could have two pages that share same base class with same view models bound to each page. Or with two view models that share same base class as well and share the data between the two view models via a service.
  • JohnHardmanJohnHardman GBUniversity mod
    edited March 2017

    @MelbourneDeveloper

    I am testing on UWP, so perhaps it is just a problem in UWP

    No, it should work on UWP. I do the majority of my testing on UWP (it's a quicker code/build/test cycle) and my testing shows no problem there.

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭
    edited March 2017

    @JohnHardman said:
    @MelbourneDeveloper

    I am testing on UWP, so perhaps it is just a problem in UWP

    No, it should work on UWP. I do the majority of my testing on UWP (it's a quicker code/build/test cycle) and my testing shows no problem there.

    I can confirm that this is a UWP specific bug. It doesn't happen on Android. It works perfectly on Android.

    I'll build a sample app to prove that this is a bug.

Sign In or Register to comment.