Fragment initialization order in MvvmCross 5 Navigation Service
I have a question about the navigation service introduced in MvvmCross 5.
In version 4:
- I move from
ShowViewModel<ViewModel>()
to fragment - then
Init
the ViewModel method is called - after that the
OnCreateView
fragment method is called - There I can manipulate the view based on the ViewModel data (for example, add certain items to the view).
In version 5:
- I navigate with
await NavigationService.Navigate<ViewModel>()
-
OnCreateView
The fragment is called first - after that the method
Initialize
from the ViewModel. - This does not result in the ViewModel being missing data when creating the fragment view.
Is this a bug or an asynchronous navigation feature? If this is the case, is there a better way to manipulate the view of the fragments based on the ViewModel data?
source to share
Is this a bug or an asynchronous navigation feature?
It was developed but has since been revised (v5.0.4), see below for stream changes.
If this is the case, is there a better way to manipulate a fragment based on the ViewModel data?
Using v5.0.4 + should give the desired behavior that you expect. Where is the navigation service expected on Initialize()
your ViewModel
to complete before triggering views lifecycle events.
MvvmCross v5.0.0 - v5.0.3
The behavior you are seeing is present in MvvmCross 5.0.0-5.0.3. The stream was as follows:
-
ViewModel.Ctor
- (Selected navigation calls)
Init(parameter)
( deprecated , uses reflection, rather uses the safe Initialize type) - (Selected navigation calls)
ViewModel.ReloadState(savedState)
- (Selected navigation calls)
ViewModel.Start()
-
BeforeNavigate
(NavigationService event) -
*ViewDispatcher.ShowViewModel()
(triggers represent lifecycles) -
*ViewModel.Initialize()
-
AfterNavigate
(NavigationService event) -
BeforeClose
(NavigationService event) -
ViewDispatcher.ChangePresentation()
-
AfterClose
(NavigationService event)
MvvmCross v5.0.4 +
v5.0.4 + improved the flow and changed the navigation order:
-
ViewModel.Ctor
-
BeforeNavigate
(NavigationService event) -
*ViewModel.Initialize()
-
Init(parameter)
( deprecated , uses reflection, rather uses the safe Initialize type) -
ViewModel.ReloadState(savedState)
-
ViewModel.Start()
-
*ViewDispatcher.ShowViewModel()
(triggers represent lifecycles) -
AfterNavigate
(NavigationService event) -
BeforeClose
(NavigationService event) -
ViewDispatcher.ChangePresentation()
-
AfterClose
(NavigationService event)
Additional Information
You can check the issue on GitHub ( # 1968 ) logged in navigational order. Alternatively, you can check out the pull request ( # 1971 ) that updated the order Initialize
for version 5.0.4.
source to share