1

ViewModel A calls ShowViewModel to ViewModel B. If B calls Close(this), it closes and go back to A.

Is there a way for ViewModel A know that B was closed? Something like a CallBack?

Thanks!

Rafael Matos
  • 155
  • 1
  • 5
  • 1
    Do any of these help - http://stackoverflow.com/questions/17319529/returning-values-to-parent-viewmodel-in-mvvmcross, http://stackoverflow.com/questions/15952101/how-make-a-viewmodel-return-data-to-another and http://stackoverflow.com/questions/18405470/how-do-i-update-the-parent-viewmodel-when-child-viewmodel-is-updated ? One "gotcha" to watch out for is that on some platforms (e.g. Droid) it's not always the case that the instance of ViewModel A will still be in memory when ViewModel B is closed - the operating system may "tombstone" and reinflate the A Activity/View while B is shown. – Stuart Oct 16 '13 at 14:35
  • After watching N=12 video (http://www.youtube.com/watch?v=6QCIoIw_O4I#t=2797), I tried to use the message system to comunicate ViewModel A about the ViewModel B closing and it worked. Is this a good approach? – Rafael Matos Oct 16 '13 at 18:23

2 Answers2

2

One of the best methods solving viewmodel interdependencies is using a loosely coupled approch using the MessageBus/Event Aggregator pattern. Many of the popular MVVM frameworks (ReactiveUI for example) ship with an implementation for this.

Oliver Weichhold
  • 10,259
  • 5
  • 45
  • 87
  • MvvmCross doesn't contain a built-in Messenger. A WeakReference-based one is provided via a plugin (https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins#messenger) but you are also free to use alternatives (e.g. TinyMessenger) – Stuart Oct 16 '13 at 14:37
1

As Stuart mentioned in one of the comments, on Android there is no guarantee that activity A will still be running to receive a notification via messenger. My favorite approach to solving "notification"-like problems in MvvmCross is to design activity A and activity B to use shared data access layer and communicate via persisted state. When you start analyzing your application, you may discover that activity A may not need to know that activity B completed. Instead, it can respond to the changes in the underlying data.

Alexey
  • 732
  • 7
  • 18
  • In my case I need to refresh an entity list in ModelView A, since B contains a form the inserts a new tuple in the database. So how can I do that using your approach? Thanks! – Rafael Matos Oct 17 '13 at 14:56
  • Check http://slodge.blogspot.com/2013/11/n42-is-my-viewmodel-visible-can-i-kill.html. You can use similar approach to re-load your data from the data layer when activity A is brought to the foreground. – Alexey Nov 20 '13 at 13:24