In ViewModel
(I also read your question incorrectly, but I leave it for completeness)
Place the determineActivationStrategy() method in the ViewModel and from there you can return the name or type of activation strategy that you want to use. Example:
determineActivationStrategy() { return "invoke-lifecycle"; }
The "invoke-lifecycle" or "replace" strings will work. You can also use the typed version by importing enum activationStrategy and keeping activationStrategy.replace / activationStrategy.invokeLifecycle . They work the same way.
At RouteConfig
Or, as Marton (who gave this answer before I did) stated, you can put it directly into the route configuration as an activationStrategy property.
This approach is better suited if the strategy does not depend on any particular state of your ViewModel model and you do not want to mask your presentation model with this material.
invoke-lifecycle vs. replace
In your question you say what you want
repeat all life cycles in VM
Please note that invoke-lifecycle reuses the existing ViewModel and will only refer to the router activation life cycle, which looks like this:
canDeactivate()deactivate()canActivate(params, routeConfig, navigationInstruction)activate(params, routeConfig, navigationInstruction)
While replace throw away the existing ViewModel and call the entire ViewModel life cycle again on top of the router activation life cycle:
canDeactivate()deactivate()detached()unbind()- (new instance):
constructor() canActivate(params, routeConfig, navigationInstruction)activate(params, routeConfig, navigationInstruction)created(owningView, thisView)bind(bindingContext, overrideContext)attached()
So, if you really want to run all the stages of the ViewModel life cycle, you need to use replace .
Fred kleuver
source share