Last active
June 6, 2019 17:32
-
-
Save quentin7b/e0cba1ba4c44e57157b5df06ec2bc487 to your computer and use it in GitHub Desktop.
Android ViewModel use LiveData as an Event bus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TheActivity : AppCompatActivity() { | |
private val theSharedViewModel by viewModel<TheSharedViewModel>() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
// bla bla bla | |
theSharedViewModel.getEvent().observe(this, Observer { | |
Log.i("TheSharedEvent", "Event Triggered") | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CrewListFragment : Fragment() { | |
// Note that the viewModel is by `sharedViewModel`, which is just a way of sharing it | |
// See https://developer.android.com/topic/libraries/architecture/viewmodel#sharing | |
private val theSharedViewModel: TheSharedViewModel by sharedViewModel() | |
// This is called by something | |
private fun someActionThatShouldTriggerTheEvent() = theSharedViewModel.triggerEvent() | |
} |
Cool, was looking for something like this - thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that I used koin as dif