Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 652f79b4 authored by 62160052's avatar 62160052
Browse files

The lifecycle of a ViewModel

parent e670b142
No related branches found
No related tags found
No related merge requests found
......@@ -17,6 +17,7 @@
package com.example.android.unscramble.ui.game
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
......@@ -46,11 +47,16 @@ class GameFragment : Fragment() {
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout XML file and return a binding object instance
binding = GameFragmentBinding.inflate(inflater, container, false)
Log.d("GameFragment", "GameFragment created/re-created!")
return binding.root
}
override fun onDetach() {
super.onDetach()
Log.d("GameFragment", "GameFragment destroyed!")
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
......@@ -69,13 +75,7 @@ class GameFragment : Fragment() {
* Displays the next scrambled word.
*/
private fun onSubmitWord() {
currentScrambledWord = getNextScrambledWord()
currentWordCount++
score += SCORE_INCREASE
binding.wordCount.text = getString(R.string.word_count, currentWordCount, MAX_NO_OF_WORDS)
binding.score.text = getString(R.string.score, score)
setErrorTextField(false)
updateNextWordOnScreen()
}
/*
......@@ -83,11 +83,7 @@ class GameFragment : Fragment() {
* Increases the word count.
*/
private fun onSkipWord() {
currentScrambledWord = getNextScrambledWord()
currentWordCount++
binding.wordCount.text = getString(R.string.word_count, currentWordCount, MAX_NO_OF_WORDS)
setErrorTextField(false)
updateNextWordOnScreen()
}
/*
......
package com.example.android.unscramble.ui.game
import android.util.Log
import androidx.lifecycle.ViewModel
class GameViewModel: ViewModel() {
......@@ -12,4 +13,14 @@ class GameViewModel: ViewModel() {
private var _currentScrambledWord = "test"
val currentScrambledWord: String
get() = _currentScrambledWord
init {
Log.d("GameFragment", "GameViewModel created!")
}
override fun onCleared() {
super.onCleared()
Log.d("GameFragment", "GameViewModel destroyed!")
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment