archi icon indicating copy to clipboard operation
archi copied to clipboard

Navigation in mvvm

Open steven274 opened this issue 9 years ago • 3 comments
trafficstars

  1. I want startactivityforresult or setresult and finish activity, how can handle it in view model of activity ?
  2. Same question with view model of recyclerview.viewholder

steven274 avatar Sep 30 '16 20:09 steven274

  1. In ViewModel you can cast context to Activity(this context should be instance of Activity) :((Activity) context).startActivityForResult().
  2. In adapter: RecyclerView.ViewHolder

itemView.getContext() then do the same as case 1.

deno028 avatar Oct 01 '16 02:10 deno028

I think View Model shouldn't know anything about View, right ?

steven274 avatar Oct 03 '16 14:10 steven274

@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.

jasminsuljic avatar Nov 26 '16 00:11 jasminsuljic