0

The ViewModel is responsible for UI logic, including Navigation.
All of the examples show the Navigate method being invoked as the result of a user action, typically a button tap, in a Command method.
The Initialize method is where 'heavy lifting', like loading data from a database is supposed to be done.
Assume an app like an email reader. The 'master' view shows a RecyclerList, the user taps an entry to see the detail.

Question:
If a condition in the data (for example, finding only 0 or 1 rows) dictates that a different screen should be displayed (not the list associated with this ViewModel), how does the ViewModel navigate to a different ViewModel?
Can I simply call Navigate from inside the Initialize method?
Should I Close the current ViewModel before navigating to the 'substitute' screen?
Any concerns regarding the back stack?
Can local data be passed via the Navigate to the substitute screen?

I have seen MvvmCross : ViewModel Navigation in code, but that was 3.5 years ago, before 5.x.

Thank you, Marc

1 Answer 1

0

You have different approaches to address this but first of all I'd recommend you to look to the new Navigation.

  1. Get the data that determines the decision concerning to which ViewModel you want to navigate before the navigation occurs. So you just navigate to the correct ViewModel depending on your data (easier approach)

  2. As you mention (if you can't get your data before) on your Initialize get the data and then navigate to the ViewModel you want. Regarding closing the current ViewModel you can do that right afterwards or you can close it later (when you close the "substitute" ViewModel you also close the one in the middle) in the presenter. This is done by using a PresentationHint so that you can then (in your platform presenter) close the corresponding ViewModel.

HIH

4
  • Hi @fmaccaroni, 1. I am so new to Android development that I have never seen anything older than 5.x. 2. Adopting a framework means I must adopt the design patterns as well as the coding techniques imposed by the framework. 3. When MvvmCross 'suggests' doing heavy lifting in Initialize, where else can it be done without impacting performance? 4. Is it 'acceptable' to keep dozens, but not hundreds, of rows stored in a singleton, available to all the ViewModels? Essentially an in-memory database? 5. Will calling Navigate from inside Initialize break anything? Thx, M.
    – M. Feldman
    Feb 7, 2018 at 20:24
  • Hi, 3) here mvvmcross.com/documentation/fundamentals/… you have the whole ViewModel lifecycle. As it suggests, you should all the heavy lifting in Initialize because it's a Task so it will be done in background without blocking the main thread and freezing the UI. 4) Yes that wouldn't be a problem if the model you are storing is not too heavy, if it is I'd recommend you to use Sqlite or Akavache as a light database.
    – fmaccaroni
    Feb 9, 2018 at 23:02
  • 5) It should'nt break anything, you just need to be careful with the backstack because you will still have the view in the middle from where you call the Navigate. So make sure you close it when you close the next one if you need it
    – fmaccaroni
    Feb 9, 2018 at 23:04
  • I am using Sqlite as the actual database storage mechanism. I will experiment with both solutions, and report back with the results. Thank you, Marc.
    – M. Feldman
    Feb 10, 2018 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.