Forum Xamarin.Android

How to Open a view model which is present in Back stack.

Ravi_98Ravi_98 Member ✭✭

In MVVM cross-application, how to check if a view model is available in Back stack? If available how can I Open that view Model? (Android)

Answers

  • AndyFlisherAndyFlisher GBBeta, University ✭✭✭✭

    Not sure that you can, and if it's possible then it's risky as Activities can be killed and closed by external factors, you're normally better off passing any variables you want in bundles from one Activity / Fragment to another and then creating a new instance of the viewmodel.

    Eg pass a record Id from the original Activity on the backstack to the new Activity when you navigate, and then in the new Activity create a new instance of the viewModel, pass the the record Id and bingo you've mirrored what you had before.

    The alternative - which polarises opinion - is you can look at the ServiceContainer method which in effect allows you to register the viewModel in Activity 1 and resolve (access) it in Activity 2 - almost like a global viewModel

    Both are complex topics but worth investigating to see which might work best for your setup.

    HTH

Sign In or Register to comment.