Day65 of #100DaysOfCode
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 ainit
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 theonCleared()
method. TheViewModel
is destroyed when the associated fragment is detached, or when the activity is finished. Right before theViewModel
is destroyed, theonCleared()
callback is called. - Then we will add a log statement inside
onCleared()
to track theGameViewModel
lifecycle.
override fun onCleared() {
super.onCleared()
Log.d("GameFragment", "GameViewModel destroyed!")
}
- In
GameFragment
insideonCreateView()
, after we get a reference to the binding object, we add a log statement to log the creation of the fragment. TheonCreateView()
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 theonDetach()
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 theGameViewModel
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 callbackonCleared()
is called. Now theGameFragment
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 ;)