0
0
Android Kotlinmobile~10 mins

ViewModel creation in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a ViewModel class named MyViewModel.

Android Kotlin
class MyViewModel : [1]() {
}
Drag options to blanks, or click blank then click option'
AViewModel
BActivity
CFragment
DApplication
Attempts:
3 left
💡 Hint
Common Mistakes
Extending Activity or Fragment instead of ViewModel.
Forgetting to extend any class.
2fill in blank
medium

Complete the code to get a ViewModel instance inside an Activity.

Android Kotlin
val viewModel = ViewModelProvider(this).[1](MyViewModel::class.java)
Drag options to blanks, or click blank then click option'
Abuild
Bget
Ccreate
DnewInstance
Attempts:
3 left
💡 Hint
Common Mistakes
Using create() or newInstance() which do not exist in ViewModelProvider.
Forgetting to pass the ViewModel class.
3fill in blank
hard

Fix the error in the ViewModel class declaration to use the correct import for ViewModel.

Android Kotlin
import androidx.lifecycle.[1]

class MyViewModel : ViewModel() {
}
Drag options to blanks, or click blank then click option'
ALifecycleOwner
BViewModelProvider
CViewModel
DLiveData
Attempts:
3 left
💡 Hint
Common Mistakes
Importing ViewModelProvider instead of ViewModel.
Importing unrelated lifecycle classes.
4fill in blank
hard

Fill both blanks to create a ViewModel with a factory inside a Fragment.

Android Kotlin
val viewModel = ViewModelProvider(this, [1]()).[2](MyViewModel::class.java)
Drag options to blanks, or click blank then click option'
AMyViewModelFactory
Bget
Ccreate
DViewModelProviderFactory
Attempts:
3 left
💡 Hint
Common Mistakes
Using create() instead of get().
Passing wrong factory class.
5fill in blank
hard

Fill all three blanks to create a ViewModel with saved state support inside an Activity.

Android Kotlin
val viewModel = ViewModelProvider(this, [1](application, [2])).[3](MyViewModel::class.java)
Drag options to blanks, or click blank then click option'
ASavedStateViewModelFactory
BsavedStateRegistryOwner
Cget
DViewModelStoreOwner
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong factory class.
Forgetting to pass savedStateRegistryOwner.
Using create() instead of get().