Skip to content

Instantly share code, notes, and snippets.

@SabagRonen
Created June 1, 2018 16:45
Show Gist options
  • Save SabagRonen/fdb5679738dae614346d6914d068bd99 to your computer and use it in GitHub Desktop.
Save SabagRonen/fdb5679738dae614346d6914d068bd99 to your computer and use it in GitHub Desktop.
What Problems Exist With Multi Activities blog post
class SharedUiData
class FragmentInteractionViewModel : ViewModel() {
val interactionLiveData = MutableLiveData<SharedUiData>()
}
class FragmentA : Fragment() {
fun publishNextUiDataToNextScreen(sharedUiData: SharedUiData) {
val viewModel = ViewModelProviders.of(activity)
.get(FragmentInteractionViewModel::class.java)
viewModel.interactionLiveData.value = sharedUiData
}
}
class FragmentB : Fragment() {
override fun onViewCreated(
view: View?,
savedInstanceState: Bundle?
) {
super.onViewCreated(view, savedInstanceState)
val viewModel = ViewModelProviders.of(activity)
.get(FragmentInteractionViewModel::class.java)
viewModel.interactionLiveData.observe({lifecycle}) {
// do something with SharedUiData
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment