Day63 of #100DaysOfCode
Hii folks 🙌
Today I will be starting a new pathway in which I’ll learn to Store data in ViewModel.
Unit 3: Navigation
Pathway 2: Architecture componentsSource
https://developer.android.com/courses/android-basics-kotlin/course
We will develop the Unscramble app which is a single player word scrambler game. The app displays one scrambled word at a time, and the player has to guess the word using all the letters from the scrambled word.
We will start by downloading the repo from the given link and import the project in the Android Studio and set up the project.
We will learn about the App architecture and it’s basic portion of the architecture.
The UI controller
control the UI by drawing views on the screen, capturing user events, and anything else ealted to the UI that the user interacts with.
And the ViewModel
is a model of the app data that is displayed in the views. Models are components that are responsible for handling the data for an app. The ViewModel
stores the app related data that isn't destroyed when activity or fragment is destroyed and recreated by the Android framework.
To summarize:
- Fragment / activity (UI controller) responsibilities
Activities and fragments are responsible for drawing views and data to the screen and responding to the user events.
- ViewModel responsibilities
ViewModel
is responsible for holding and processing all the data needed for the UI. It should never access our view hierarchy (like view binding object) or hold a reference to the activity or the fragment.
We will add a ViewModel
to our app to store our app data(scrambled word, word count, and score).
// ViewModel
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
And create a new Kotlin class file called GameViewModel
and attach the ViewModel to the Fragments
class GameViewModel : ViewModel() {
private val viewModel: GameViewModel by viewModels()
}
That is all for Day63 ✅
Thanks for reading, See you tomorrow!
If you are reading my #100Days Journey, feel free to drop by ;)