mirror of
https://github.com/Skysamara/android-kotlin-fundamentals-apps.git
synced 2025-12-05 22:06:04 +00:00
Fix: Changing the Lifecycle owner to fragement view lifecycle.
Changing the Lifecycle owner to fragement view lifecycle instead of fragment lifecycle for Databinding and Livedata. Ref issue: https://github.com/google-developer-training/android-kotlin-fundamentals-apps/issues/15
This commit is contained in:
parent
57995e8080
commit
59d36c7d6c
@ -57,12 +57,12 @@ class GameFragment : Fragment() {
|
||||
// to all the data in the VieWModel
|
||||
binding.gameViewModel = viewModel
|
||||
|
||||
// Specify the current activity as the lifecycle owner of the binding.
|
||||
// Specify the fragment view as the lifecycle owner of the binding.
|
||||
// This is used so that the binding can observe LiveData updates
|
||||
binding.lifecycleOwner = this
|
||||
binding.lifecycleOwner = viewLifecycleOwner
|
||||
|
||||
// Observer for the Game finished event
|
||||
viewModel.eventGameFinish.observe(this, Observer<Boolean> { hasFinished ->
|
||||
viewModel.eventGameFinish.observe(viewLifecycleOwner, Observer<Boolean> { hasFinished ->
|
||||
if (hasFinished) gameFinished()
|
||||
})
|
||||
|
||||
|
||||
@ -55,12 +55,12 @@ class ScoreFragment : Fragment() {
|
||||
.get(ScoreViewModel::class.java)
|
||||
binding.scoreViewModel = viewModel
|
||||
|
||||
// Specify the current activity as the lifecycle owner of the binding.
|
||||
// Specify the fragment view as the lifecycle owner of the binding.
|
||||
// This is used so that the binding can observe LiveData updates
|
||||
binding.lifecycleOwner = this
|
||||
binding.lifecycleOwner = viewLifecycleOwner
|
||||
|
||||
// Navigates back to game when button is pressed
|
||||
viewModel.eventPlayAgain.observe(this, Observer { playAgain ->
|
||||
viewModel.eventPlayAgain.observe(viewLifecycleOwner, Observer { playAgain ->
|
||||
if (playAgain) {
|
||||
findNavController().navigate(ScoreFragmentDirections.actionRestart())
|
||||
viewModel.onPlayAgainComplete()
|
||||
|
||||
@ -54,17 +54,17 @@ class GameFragment : Fragment() {
|
||||
viewModel = ViewModelProviders.of(this).get(GameViewModel::class.java)
|
||||
|
||||
/** Setting up LiveData observation relationship **/
|
||||
viewModel.word.observe(this, Observer { newWord ->
|
||||
viewModel.word.observe(viewLifecycleOwner, Observer { newWord ->
|
||||
binding.wordText.text = newWord
|
||||
})
|
||||
|
||||
viewModel.score.observe(this, Observer { newScore ->
|
||||
viewModel.score.observe(viewLifecycleOwner, Observer { newScore ->
|
||||
binding.scoreText.text = newScore.toString()
|
||||
|
||||
})
|
||||
|
||||
// Observer for the Game finished event
|
||||
viewModel.eventGameFinish.observe(this, Observer<Boolean> { hasFinished ->
|
||||
viewModel.eventGameFinish.observe(viewLifecycleOwner, Observer<Boolean> { hasFinished ->
|
||||
if (hasFinished) gameFinished()
|
||||
})
|
||||
|
||||
|
||||
@ -56,14 +56,14 @@ class ScoreFragment : Fragment() {
|
||||
.get(ScoreViewModel::class.java)
|
||||
|
||||
// Add observer for score
|
||||
viewModel.score.observe(this, Observer { newScore ->
|
||||
viewModel.score.observe(viewLifecycleOwner, Observer { newScore ->
|
||||
binding.scoreText.text = newScore.toString()
|
||||
})
|
||||
|
||||
binding.playAgainButton.setOnClickListener { viewModel.onPlayAgain() }
|
||||
|
||||
// Navigates back to game when button is pressed
|
||||
viewModel.eventPlayAgain.observe(this, Observer { playAgain ->
|
||||
viewModel.eventPlayAgain.observe(viewLifecycleOwner, Observer { playAgain ->
|
||||
if (playAgain) {
|
||||
findNavController().navigate(ScoreFragmentDirections.actionRestart())
|
||||
viewModel.onPlayAgainComplete()
|
||||
|
||||
@ -57,9 +57,9 @@ class GameFragment : Fragment() {
|
||||
// to all the data in the VieWModel
|
||||
binding.gameViewModel = viewModel
|
||||
|
||||
// Specify the current activity as the lifecycle owner of the binding.
|
||||
// Specify the fragment view as the lifecycle owner of the binding.
|
||||
// This is used so that the binding can observe LiveData updates
|
||||
binding.lifecycleOwner = this
|
||||
binding.lifecycleOwner = viewLifecycleOwner
|
||||
|
||||
|
||||
// Observer for the Game finished event
|
||||
|
||||
@ -55,9 +55,9 @@ class ScoreFragment : Fragment() {
|
||||
.get(ScoreViewModel::class.java)
|
||||
binding.scoreViewModel = viewModel
|
||||
|
||||
// Specify the current activity as the lifecycle owner of the binding.
|
||||
// Specify the fragment view as the lifecycle owner of the binding.
|
||||
// This is used so that the binding can observe LiveData updates
|
||||
binding.lifecycleOwner = this
|
||||
binding.lifecycleOwner = viewLifecycleOwner
|
||||
|
||||
// Navigates back to game when button is pressed
|
||||
viewModel.eventPlayAgain.observe(this, Observer { playAgain ->
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user