mirror of
https://github.com/Skysamara/android-kotlin-fundamentals-apps.git
synced 2025-12-06 06:16:03 +00:00
Merge pull request #24 from google-developer-training/developer
Fix: Changing the Lifecycle owner to fragment view lifecycle.
This commit is contained in:
commit
8d7a62415b
@ -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()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@ -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()
|
||||||
|
|||||||
@ -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()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@ -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()
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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 ->
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user