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:
Jhansi 2020-01-27 15:07:54 -08:00
parent 57995e8080
commit 59d36c7d6c
6 changed files with 15 additions and 15 deletions

View File

@ -57,12 +57,12 @@ class GameFragment : Fragment() {
// to all the data in the VieWModel // to all the data in the VieWModel
binding.gameViewModel = 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 // This is used so that the binding can observe LiveData updates
binding.lifecycleOwner = this binding.lifecycleOwner = viewLifecycleOwner
// Observer for the Game finished event // Observer for the Game finished event
viewModel.eventGameFinish.observe(this, Observer<Boolean> { hasFinished -> viewModel.eventGameFinish.observe(viewLifecycleOwner, Observer<Boolean> { hasFinished ->
if (hasFinished) gameFinished() if (hasFinished) gameFinished()
}) })

View File

@ -55,12 +55,12 @@ class ScoreFragment : Fragment() {
.get(ScoreViewModel::class.java) .get(ScoreViewModel::class.java)
binding.scoreViewModel = viewModel 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 // 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 // Navigates back to game when button is pressed
viewModel.eventPlayAgain.observe(this, Observer { playAgain -> viewModel.eventPlayAgain.observe(viewLifecycleOwner, Observer { playAgain ->
if (playAgain) { if (playAgain) {
findNavController().navigate(ScoreFragmentDirections.actionRestart()) findNavController().navigate(ScoreFragmentDirections.actionRestart())
viewModel.onPlayAgainComplete() viewModel.onPlayAgainComplete()

View File

@ -54,17 +54,17 @@ class GameFragment : Fragment() {
viewModel = ViewModelProviders.of(this).get(GameViewModel::class.java) viewModel = ViewModelProviders.of(this).get(GameViewModel::class.java)
/** Setting up LiveData observation relationship **/ /** Setting up LiveData observation relationship **/
viewModel.word.observe(this, Observer { newWord -> viewModel.word.observe(viewLifecycleOwner, Observer { newWord ->
binding.wordText.text = newWord binding.wordText.text = newWord
}) })
viewModel.score.observe(this, Observer { newScore -> viewModel.score.observe(viewLifecycleOwner, Observer { newScore ->
binding.scoreText.text = newScore.toString() binding.scoreText.text = newScore.toString()
}) })
// Observer for the Game finished event // Observer for the Game finished event
viewModel.eventGameFinish.observe(this, Observer<Boolean> { hasFinished -> viewModel.eventGameFinish.observe(viewLifecycleOwner, Observer<Boolean> { hasFinished ->
if (hasFinished) gameFinished() if (hasFinished) gameFinished()
}) })

View File

@ -56,14 +56,14 @@ class ScoreFragment : Fragment() {
.get(ScoreViewModel::class.java) .get(ScoreViewModel::class.java)
// Add observer for score // Add observer for score
viewModel.score.observe(this, Observer { newScore -> viewModel.score.observe(viewLifecycleOwner, Observer { newScore ->
binding.scoreText.text = newScore.toString() binding.scoreText.text = newScore.toString()
}) })
binding.playAgainButton.setOnClickListener { viewModel.onPlayAgain() } binding.playAgainButton.setOnClickListener { viewModel.onPlayAgain() }
// Navigates back to game when button is pressed // Navigates back to game when button is pressed
viewModel.eventPlayAgain.observe(this, Observer { playAgain -> viewModel.eventPlayAgain.observe(viewLifecycleOwner, Observer { playAgain ->
if (playAgain) { if (playAgain) {
findNavController().navigate(ScoreFragmentDirections.actionRestart()) findNavController().navigate(ScoreFragmentDirections.actionRestart())
viewModel.onPlayAgainComplete() viewModel.onPlayAgainComplete()

View File

@ -57,9 +57,9 @@ class GameFragment : Fragment() {
// to all the data in the VieWModel // to all the data in the VieWModel
binding.gameViewModel = 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 // This is used so that the binding can observe LiveData updates
binding.lifecycleOwner = this binding.lifecycleOwner = viewLifecycleOwner
// Observer for the Game finished event // Observer for the Game finished event

View File

@ -55,9 +55,9 @@ class ScoreFragment : Fragment() {
.get(ScoreViewModel::class.java) .get(ScoreViewModel::class.java)
binding.scoreViewModel = viewModel 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 // 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 // Navigates back to game when button is pressed
viewModel.eventPlayAgain.observe(this, Observer { playAgain -> viewModel.eventPlayAgain.observe(this, Observer { playAgain ->