On the ViewModel
(I misread your question at first too, but I'm leaving this in for completeness)
Place a method determineActivationStrategy()
on the ViewModel and from there you can return the name or type of the activation strategy you wish to use. Example:
determineActivationStrategy() {
return "invoke-lifecycle";
}
The strings "invoke-lifecycle"
or "replace"
will work. You can also use the typed version by importing the enum activationStrategy
and returing activationStrategy.replace
/ activationStrategy.invokeLifecycle
. They work the same.
In the RouteConfig
Or, as stated by Marton (who gave this answer before I did), you can put it directly in route config as the property activationStrategy
.
This approach is better suited if the strategy does not depend on any particular state of your ViewModel and you don't wish to litter your view model with this stuff.
invoke-lifecycle vs. replace
In your question you say you want to
re-run the all the life cycles in the VM
Note that invoke-lifecycle
reuses the existing ViewModel and will only invoke the router activation lifecycle, which is as follows:
canDeactivate()
deactivate()
canActivate(params, routeConfig, navigationInstruction)
activate(params, routeConfig, navigationInstruction)
Whereas replace
will throw away the existing ViewModel and invoke the whole ViewModel lifecycle again on top of the router activation lifecycle:
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 ViewModel lifecycle steps, you'll need to use replace
.