[XF] OnNavigatedFrom not called when using absolute URI
- Prism version: 6.1.0-pre5
- Xamarin version: 2.3.0.46-pre3
Repro steps
When navigating to a page via absolute uri in order to "Reset" the navigation stack OnNavigatedFrom isn't called.
navigationService.NavigateAsync(new System.Uri("ms-app:///HomePage", System.UriKind.Absolute)));
Not sure if this is working as designed or not, or if there's a new better way to reset the stack, but it was unexpected to have a different behavior depending on how we use NavigateAsync.
Good catch! I'll get that fixed.
Thanks!
Not sure if this is related or me not understanding something but I found this because in the constructor of my viewmodel I am using Prism.Events to subscribe to an event telling the viewmodel to refresh if a datasync occurred.
I found that after navigating away from the view(page) the method in the viewmodel that I had subscribed to was still firing off even though I've navigated away from the page. I thought that viewmodels weren't cached and so I shouldn't have had to unsubscribe from the event since Prism.Events handles all that stuff.
To deal with it, I just put an Unsubscribe to the event in the OnNavigatedFrom to ensure it goes away, which lead to finding out in certain situations OnNavigatedFrom wasn't firing.
Navigating away from a View does not mean that it is no longer in memory. Especially if you called NavgateAsync. Your entire navigation stack remains in memory.
Do we have an option other than NavigateAsync in the latest Prism XF preview? We had to update all the navigate calls to navigateasync after updating and don't see an alternative. Back in December we had talked about caching views/viewmodels and how when you navigate to a page the viewmodel has to be recreated, which caused us to completely change how we navigated to prevent ever increasing memory usage. Part of that is in certain situations we need to clear the navigation stack by navigating to an absolute uri. Has something changed since then that I missed?
I guess the big question I have is, if I have a view and I navigate away from a view using an absolute URI with NavigateAsync, that view shouldn't be in the stack and we can no longer go back to the page, how would that view ever get cleaned up from memory? If calling NavigationAsync means it's still in memory even when you use an absolute URI (and any other views that were (but are no longer) in the navigation stack for that matter), wouldn't you end up with entire views/viewmodels orphaned in memory every time you used absolute navigation?
Thanks again for your help.
No, there is a misunderstanding. If you are navigating forward and just adding pages to the stack, then of course your previous pages remain on the current stack and in memory. If you navigate back to pop pages off the stack, or use an absolute URI then those pages are just waiting to be GC'd. I have no control over when that happens. Xamarin.Forms manages the collection of their pages when removed from the navigation stack. When those pages are collected, then then IEventAgregator will not hold a reference to them as the weak event handlers will be released.
Also, the ViewModels are only recreated if you are adding a page to the stack using NavigateAsync, not if going back to a previous page with GoBackAsync
Ok, we're on the same page, and that's how I thought it worked. Thanks for the clarification.
Sent from my Windows Phone
From: Brian Lagunasmailto:[email protected] Sent: 6/1/2016 4:21 PM To: PrismLibrary/Prismmailto:[email protected] Cc: Jeff Dalbymailto:[email protected]; Authormailto:[email protected] Subject: Re: [PrismLibrary/Prism] [XF] OnNavigatedFrom not called when using absolute URI (#616)
Also, the ViewModels are only recreated if you are adding a page to the stack using NavigateAsync, not if going back to a previous page with GoBackAsync
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/PrismLibrary/Prism/issues/616#issuecomment-223142611, or mute the threadhttps://github.com/notifications/unsubscribe/APAwWJzAjlxxHZO0VYrIp8rlT5y_owbXks5qHgXXgaJpZM4Ir3eC.
I am on some page and I navigate to another page using absolute URI, the OnNavigatedFrom method is not fired. I implemented the Timer in this ViewModel and using OnNavigatedFrom to unregistered it. this issue makes the timer run background all the time which affect the app performance. is there any solution for it?
Best Thanks!
@dationguyen you have not provided enough information for us to determine what the problem may be. If you want to clean up your ViewModels you should be using IDestructible
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.