Prism: [XF] OnNavigatedTo() not triggered when using the OS back button instead of the NavigationService

Created on 24 Aug 2016  路  29Comments  路  Source: PrismLibrary/Prism

Package info

  • Platform: All
  • Prism version: 6.2
  • Xamarin version (if applicable): 4.1.2.18
  • Windows 10 SDK version (if applicable): 14393
  • Other version info:

    Repro steps

1) Create a page with its own ViewModel, let it inherit from INavigationAware inteface and implement the OnNavigatedTo() method
2) Observe that, when you navigate to the page using the NavigationService, the OnNavigatedTo() method is properly called
3) Now add another page and, using the NavigationService, navigate from the first page to the new one.
4) Now go back using the embedded OS feature (virtual back button on Android or Windows, UI back button on iOS).

Expected result

The OnNavigatedTo() method of the ViewModel of the first page is invoked and executed

Current result

Since the navigation isn't triggered by the NavigationService, the OnNavigatedTo() method isn't invoked. If you go back calling the GoBack() method of the NavigationService, instead, the method is properly invoked. This issue can cause problems when, for example, you want to reload the page's data every time the user navigates to it.

XF enhancement

Most helpful comment

I'm tired of waiting on Xamarin, so I am working on my own solution.

All 29 comments

This is a known limitation of the Xamarin.Forms API and is also a duplicate of #634

@qmatteoq while you wait for this to be fixed, you may consider either implementing a behavior you can add to your NavigationPage or implement something directly on your NavigationPage, either as an event handler or override OnBackButtonPressed.

I'm tired of waiting on Xamarin, so I am working on my own solution.

Does this work on windows phone 8.1? It works on android for my app, but not windows phone 8.1 hardware back button.

Windows phone is no longer officially supported. Not even Xamarin provides templates for Windows or Windows Phone 8.1 anymore. I would suggest not creating an 8.1 project because it's end of life is very near.

Haha I would prefer that too. Not my call though:(

So how was this addressed exactly? What API do we have to handle theses cases?

I'm on 6.3 (stable) and this still doesn't work.

Yes, it works. When using the hardware/software buttons only the OnNavigatedFrom and OnNavigatedTo methods are called.

I did two things and showed my product owner

1) I disabled the back button in platform specific project, and said look, the only way to navigate back is via the back button in the top left corner in our nav bar so that iOS, android, and win phone 8.1 all behave the same.

Well PO didn't like that very much,so...
2)I used the event agrigator and published it when the back button was detected in the platform specific project. Then in my baseViewmodel I listen for it and just call navigation service.gobackAsync()

Oh I think I misunderstood, I was answering for just the winphone 8.1 scenario. This works in prism perfectly for android and iOS

@brianlagunas doesn't work for me. What I'm doing is:

I navigated to NavigationPage/ViewA. Then, from ViewA (inside the NavigationPage) I call Navigate to ViewB. Then from ViewB I go back. ViewA.NavigatedTo does not get called.

Looking at the source code that (should have) fixed this, my assumption is that the magic is in the NavigationPageSystemGoBackBehavior, right?
Could this be happening because ViewA is a ContentPage calling ViewB which is also a ContentPage?

Edit: I'm trying to work on an isolated repro

I don't know what to tell you. It works perfectly for me.

@brianlagunas For a big project whats the best to use. autofac or unity ?

I'd use Unity over Autofac any day, but DryIoc is a good option too.

@brianlagunas Killing me here. Thank you. Sorry, I took over the thread.

@brianlagunas I think I found the culprit... I have a BaseNavigationPage that inherits from IconNavigationPage (which is a NavigationPage after all)...

public partial class BaseNavigationPage : IconNavigationPage
    {
        public BaseNavigationPage(Page page) : base(page)
        {
            InitializeComponent();
        }
    }

so the NavigationPageSystemGoBackBehavior does not get applied, because it is not a NavigationPage directly. This is the check that fails.

Makes sense?

If that is the case, I could just inherit from PageNavigationService and override the ApplyPageBehaviors, right?

Did you actually test this, or are you guessing? Why don't you just add the behavior in your navigationPage ctor?

I was doing exactly that :)

Now I can confirm that adding the behavior to the BaseNavigationPage ctor worked.

Do you think the check should be changed to consider the whole inheritance? I could send a PR if you think that is a change that makes sense.

That would probably be a good thing. I am actually working on abstracting the page behaviors out of the navigation service and into an IPageBehaviorFactory to make it much easier to add new behaviors. Feel free to submit your PR, and I will take it and apply it to my refactored version I am working on now.

Thinking about it, it's really weird that it didn't work. Since the NavigationPage is in the inheritance tree the "is" check should have worked.

In that code sample there isn't a navigation page. Just something called basenavigation page

But the IconNavigationPage is a NavigationPage

Indeed, the is should not be a problem.

But for some reason the Behavior was not applied. Checking it at the constructor shows the Behaviors are empty, and adding it fixed the problem.

For reference, the IconNavigationPage doesn't do much.

Hello, for the problem pointed out in this issue, do you have a permanent or temporary solution?

In my case, when I press the back button of title bar, I need to refresh the listview of the previous page.

@jacksonveroneze As you can see, this issue has been closed as fixed.

Sorry @brianlagunas

My beginner's knowledge leaves me limited still. I'm using version 7 of the prism. But behavior is the same. I did not understand what should be done.

Hello,
Why don't Prism for XF has its own NavigationPage ?
This is what I came with :
`
///

/// NavigationPageExtended allows the viewModels which implement INavigationAware to still work with native navigation (GoBack button).
/// </summary>
/// <remarks>
/// Works only with Navigated methods. Navigating won't.
/// </remarks>
public class NavigationPageExtended : NavigationPage
{
    public NavigationPageExtended(Page root) : base(root)
    {
        Behaviors.Add(new Prism.Behaviors.NavigationPageSystemGoBackBehavior());
    }
}

`
Loving Prism a bit more each day I use it :) you did an awesome framework.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brianlagunas picture brianlagunas  路  4Comments

vardansargsyan92 picture vardansargsyan92  路  3Comments

weitzhandler picture weitzhandler  路  6Comments

tiagodenoronha picture tiagodenoronha  路  4Comments

ylatuya picture ylatuya  路  6Comments