Merge pull request #24 from google-developer-training/developer

Fix: Changing the Lifecycle owner to fragment view lifecycle.
This commit is contained in:
jtavva 2020-01-29 10:38:03 -08:00 committed by GitHub
commit 8d7a62415b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
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()
})

View File

@ -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()

View File

@ -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()
})

View File

@ -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()

View File

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

View File

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