Saving Data on Android



Download 19,28 Mb.
Pdf ko'rish
bet174/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   170   171   172   173   174   175   176   177   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

ViewModel
 is created.
And that's it! Your 
ViewModel
s are ready. Now, you need to create your 
View
s.
Defining your Views
As mentioned at the beginning of this chapter, the 
ViewModel
 is scoped to the 
lifecycle of an 
Activity
 or 
Fragment
 which means that it will live as long as its 
scope is still alive.
To create a 
ViewModel
 you usually call the 
ViewModelProviders.of(Scope).get(Type)
 which contains several utility 
methods that help you attach a 
ViewModel
 to a certain lifecycle and keep track of its 
state. This is how the code would look:
viewModel = 
ViewModelProviders.of(
this
).
get
(MainViewModel::
class
.
java
)
The only problem with the above approach is that the 
ViewModelProviders
 is 
responsible for creating our 
ViewModel
s and, as such, it can't call their custom 
constructors. By default, 
ViewModelProviders
 will always call the empty 
constructor using 
get()
. This is a problem because you need to pass the repository 
as a parameter.
Saving Data on Android Chapter 9: Using Room with Google's Architecture Components
raywenderlich.com
162


There are several approaches to solve the above problem but the usual way of doing 
it is to create a factory for the 
ViewModel
 and to pass it to 
ViewModelProviders
. To 
keep things short and to the point, a couple of extension functions for the Activity 
and Fragment classes have already been prepared for you that automatically take 
care of doing all of this for you. If you want to take a look at the source code just 
open 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   170   171   172   173   174   175   176   177   ...   292




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish