Day65 of #100DaysOfCode

Kushagra Kesav
3 min readApr 12, 2022

--

#CodeTogether Day 65/100

Hii folks 🙌

Today I will be continuing the same pathway in which we’ll learn about the lifecycle of a ViewModel.

Unit 3: Navigation

Pathway 2: Architecture componentsSource

https://developer.android.com/courses/android-basics-kotlin/course

The framework keeps the ViewModel alive as long as the scope of the activity or fragment is alive. A ViewModel is not destroyed if its owner is destroyed for a configuration change, such as screen rotation. The new instance of the owner reconnects to the existing ViewModel instance, as illustrated by the following diagram:

Understand ViewModel lifecycle

We will add logging in the GameViewModel and GameFragment to better understand the lifecycle of the ViewModel.

  • In GameViewModel.kt we will add a init block with a log statement.
class GameViewModel : ViewModel() {
init {
Log.d("GameFragment", "GameViewModel created!")
}
...
}

Initializer blocks are prefixed with the init keyword followed by the curly braces {}. This block of code is run when the object instance is first created and initialized.

  • In the GameViewModel class, override the onCleared() method. The ViewModel is destroyed when the associated fragment is detached, or when the activity is finished. Right before the ViewModel is destroyed, the onCleared() callback is called.
  • Then we will add a log statement inside onCleared() to track the GameViewModel lifecycle.
override fun onCleared() {
super.onCleared()
Log.d("GameFragment", "GameViewModel destroyed!")
}
  • In GameFragment inside onCreateView(), after we get a reference to the binding object, we add a log statement to log the creation of the fragment. The onCreateView() callback will be triggered when the fragment is created for the first time and also every time it is re-created for any events like configuration changes.
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = GameFragmentBinding.inflate(inflater, container, false)
Log.d("GameFragment", "GameFragment created/re-created!")
return binding.root
}
  • In GameFragment, we override the onDetach() callback method, which will be called when the corresponding activity and fragment are destroyed.
override fun onDetach() {
super.onDetach()
Log.d("GameFragment", "GameFragment destroyed!")
}
  • Now we will enable the auto-rotate setting on our device or emulator and change the screen orientation a few times. The GameFragment is destroyed and recreated each time, but the GameViewModel is created only once, and it is not re-created or destroyed for each call.
  • Now we will exit the game or navigate out of the app using the back arrow. The GameViewModel is destroyed, and the callback onCleared() is called. Now the GameFragment is destroyed.
com.example.android.unscramble D/GameFragment: GameViewModel destroyed!
com.example.android.unscramble D/GameFragment: GameFragment destroyed!

That is all for Day65 ✅

Thanks for reading, See you tomorrow!

If you are reading my #100Days Journey, feel free to drop by ;)

--

--

Kushagra Kesav
Kushagra Kesav

Written by Kushagra Kesav

Developer Relations | Communities | Software Engineering | https://www.linkedin.com/in/kushagrakesav

No responses yet