archi
archi copied to clipboard
Navigation in mvvm
- I want startactivityforresult or setresult and finish activity, how can handle it in view model of activity ?
- Same question with view model of recyclerview.viewholder
- In ViewModel you can cast context to Activity(this context should be instance of Activity) :((Activity) context).startActivityForResult().
- In adapter: RecyclerView.ViewHolder
itemView.getContext() then do the same as case 1.
I think View Model shouldn't know anything about View, right ?
@steven274 i share your opinion about view model not knowing anything about view, even about platform specific stuff. What i tend to do when dealing with use cases you described (startactivityforresult) is to create interface: <view_model>Delegate with implementation from activity. Then you can invoke: delegate.doSomethingWhatRequiresStartActivityForResult() and from onActivityResult (in host activity) invoke viewModel.success(response).
With this approach you can mock <view_model>Delegate implementation and test viewModel with local tests, its just plan old platform independent java code.