I have a simple project MvvmCross+Xamarin.Android I'm using standard MvvmCross navigation to display next view.
Let's say that there are two activities with related ViewModels: First, Second. Scenario:
1. start the - FirstViewModel is displayed
2. go to SecondViewModel
3. press the hardware back button
As a result I have both ViewModel working in background (based on weak and strong messenger events). If I want to stop it I can call GC.Collect.
I have two options here to have my view model properly collected and with both I have concerns:
1. use messenger + weak references
2. use messenger + strong references
Weak references based events don't really work for me because I could lost my subscriptions even for ACTIVE ViewModel (which is bound to the View currently displayed). That will cause my view to get into state when no subscription based events will be received and my ViewModel will not be responsive to Services in background (like incoming messages)
Strong references approach based events work better but in this case I:
1. have to control manually moment when to subscribe/unsubscribe to events
2. have to make sure that I always unsubscribing from events on View deactivation/destroy
3. it seems that I also have to implement 4 methods and call it from native code on view activated/deactivate, created/destroyed.
- Do I have correct assumptions?
- Are there any advices on how to handle this in more reliable way?
PS: Probably it is better to implement those methods in MvvmCross SDK and as long as we inherit all our view from base MvvmCross classes (MvxActivity, MvxViewController) have it wired up automatically.
PPS: I did read this post and I'm still thinking it should be implemented somewhere on SDK level
You can download sample project here: xLeak Source Code