Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Inside Jetpack Architecture Components

8766b19fe1c1f8475e233c910ed6440f?s=47 kobito-kaba
September 26, 2018

Inside Jetpack Architecture Components

How LifecycleObservers, LiveData, ViewModels work

8766b19fe1c1f8475e233c910ed6440f?s=128

kobito-kaba

September 26, 2018
Tweet

Transcript

  1. )JSPZVLJ.PSJ !NPSJESPJE *OTJEF+FUQBDL "SDIJUFDUVSF$PNQPOFOUT

  2.  "7FOUVSFCFUXFFO4PGUCBOLBOE:BIPP  0OFPGCJHHFTUTFBSDIJO+BQBO

  3. "SDIJUFDUVSF $PNQPOFOUT

  4. -JGFDZDMFT -JWF%BUB 7JFX.PEFM 3PPN 1BHJOH %BUB#JOEJOH /BWJHBUJPO 8PSL.BOBHFS %PXOMPBE.BOBHFS .FEJB1MBZCBDL

    1FSNJTTJPOT /PUJDBUJPOT 4IBSJOH 4MJDFT "OJNBUJPO5SBOTJUJPOT "VUP 578FBS &NPKJ 'SBHNFOU -BZPVU 1BMFUUF "QQ$PNQBU "OESPJE,59 .VMUJEFY 5FTU
  5. -JGFDZDMFT -JWF%BUB 7JFX.PEFM 3PPN 1BHJOH %BUB#JOEJOH /BWJHBUJPO 8PSL.BOBHFS "QQ$PNQBU "OESPJE,59

  6. -JGFDZDMFT -JWF%BUB 7JFX.PEFM 3PPN 1BHJOH %BUB#JOEJOH /BWJHBUJPO 8PSL.BOBHFS "QQ$PNQBU "OESPJE,59

    BOESPJEBSDIMJGFDZDMF
  7. -JGFDZDMF1SPCMFNT JO"OESPJE

  8. JOFBSMZEBZT

  9. UPEBZ խ

  10. DBTF UPVDIJOHEFTUSPZFE7JFX

  11. DBTFUPVDIJOHEFTUSPZFE7JFX 

  12. DBTFUPVDIJOHEFTUSPZFE7JFX   Cute Cat

  13. DBTFUPVDIJOHEFTUSPZFE7JFX   Cute Cat 

  14. Cute Cat DBTFUPVDIJOHEFTUSPZFE7JFX     

  15. DBTFUPVDIJOHEFTUSPZFE7JFX       Cute Cat

  16. DBTFUPVDIJOHEFTUSPZFE7JFX       Cute Cat 

     
  17. DBTFUPVDIJOHEFTUSPZFE7JFX       Cute Cat 

      
  18. DBTFUPVDIJOHEFTUSPZFE7JFX       Cute Cat 

       
  19. DBTF GFUDIJOHBUFWFSZPO$SFBUF

  20. Cute Cat DBTFGFUDIJOHBUFWFSZPO$SFBUF   

  21. The Japanese Bobtail is a breed of domestic cat with

    an unusual "bobbed" tail more closely resembling the tail of a rabbit than that of other cats. The variety is native to Japan and Southeast Asia, though it is now found throughout the world. The breed has been known in Japan for centuries, and it frequently appears in traditional folklore and art. Short Hair • Brown • White • Cute Caption Japanese Bobtail Cute Cat DBTFGFUDIJOHBUFWFSZPO$SFBUF     
  22. DBTFGFUDIJOHBUFWFSZPO$SFBUF       Cute Cat

  23. DBTFGFUDIJOHBUFWFSZPO$SFBUF       Cute Cat 

      
  24. DBTFGFUDIJOHBUFWFSZPO$SFBUF       Cute Cat 

       
  25.  Cute Cat DBTFGFUDIJOHBUFWFSZPO$SFBUF      

        
  26. DBTF EJTQPTJOHJOXSPOHXBZ

  27.  DBTFEJTQPTJOHJOXSPOHXBZ Cute Cat Cute Cat

  28. Cute Cat Cute Cat  DBTFEJTQPTJOHJOXSPOHXBZ 

  29. Cute Cat Cute Cat  DBTFEJTQPTJOHJOXSPOHXBZ  

  30. The Japanese Bobtail is a breed of domestic cat with

    an unusual "bobbed" tail more closely resembling the tail of a rabbit than that of other cats. The variety is native to Japan and Southeast Asia, though it is now found throughout the world. The breed has been known in Japan for centuries, and it frequently appears in traditional folklore and art. Short Hair • Brown • White • Cute Caption Japanese Bobtail Cute Cat  DBTFEJTQPTJOHJOXSPOHXBZ       
  31. The Japanese Bobtail is a breed of domestic cat with

    an unusual "bobbed" tail more closely resembling the tail of a rabbit than that of other cats. The variety is native to Japan and Southeast Asia, though it is now found throughout the world. The breed has been known in Japan for centuries, and it frequently appears in traditional folklore and art. Short Hair • Brown • White • Cute Caption Japanese Bobtail Cute Cat  DBTFEJTQPTJOHJOXSPOHXBZ        
  32. Cute Cat Cute Cat  DBTFEJTQPTJOHJOXSPOHXBZ    

  33. Cute Cat Cute Cat  DBTFEJTQPTJOHJOXSPOHXBZ    

    
  34. 4P XIBUXFTIPVE

  35. DBTFUPVDIJOHEFTUSPZFE7JFX DBTFGFUDIJOHBUFWFSZPO$SFBUF DBTFEJTQPTJOHJOXSPOHXBZ

  36. DBTFUPVDIJOHEFTUSPZFE7JFX DBTFGFUDIJOHBUFWFSZPO$SFBUF DBTFEJTQPTJOHJOXSPOHXBZ -JWF%BUB 7JFX.PEFMT -JWF%BUB7JFX.PEFMT

  37. -JGFDZDMF0CTFSWFS

  38.  

  39.    

  40.        

  41. TVCTDSJCF TVCTDSJCF        

  42. TVCTDSJCF TVCTDSJCF        

    FWFOUIBQQFOT
  43. SFFWFOUT SFFWFOUT        

    FWFOUIBQQFOT
  44.  

  45.   

  46.    

  47.    

  48.   

  49.    

  50.     

  51.      

  52.          

  53.        

  54.        

  55.         &WFOU$IBOOFM

  56.        

  57.         1VCMJTIFS

  58.        

  59.        

  60.        

  61.      

  62. -JGFDZDMF0CTFSWFS ٥0CTFSWFMJGFDZDMFT ٥%FTJHOFEBT1VC4VC ٥#PJMFSQMBUFGSFF ٥-JGFDZDMF"XBSF$PNQPOFOU

  63. -JWF%BUB

  64.    

  65.       

  66.         

  67.   

  68.    

  69.       

  70.        

  71.         0O4UBSUFE 0O3FTVNFE

  72.          0O4UBSUFE

    0O3FTVNFE
  73.          0O1BVTFE

  74.          0O1BVTFE

    #MPDL
  75.          0O%FTUSPZFE

  76.          0O%FTUSPZFE

  77. -JWF%BUB ٥0CTFSWBCMFEBUBIPMEFS ٥-JGFDZDMF"XBSF ٥/PMFBL

  78. 7JFX.PEFM

  79.    

  80.     

  81.      

  82.       

  83.      

  84.       

  85.        

  86.     

  87.      

  88.      

  89.  /PO$POHVSBUJPO*OTUBODF

  90.      

  91.       

  92.       

  93.       

  94. 7JFX.PEFM ٥%BUBIPMEFS ٥4VSWJWFTDPOHVSBUJPODIBOHFT ٥/FWFSSFGFSFODFT$POUFYU

  95. )PXUPVTF

  96. TUFQ "EEDPNQPOFOUT

  97. dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.0.0-rc02'

    implementation 'androidx.constraintlayout:constraintlayout:1.1.3' } -JGFDZDMFT
  98. dependencies { … def lifecycle_version = "2.0.0-rc01" implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version" }

    -JWF%BUB7JFX.PEFM
  99. apply plugin : 'kotlin-kapt' dependencies { … def lifecycle_version =

    "2.0.0-rc01" implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version" kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" } -JGFDZDMF0CTFSWFS
  100. dependencies { ... implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.5" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:0.22.5" implementation "com.squareup.okhttp3:okhttp:3.10.0" implementation

    "com.squareup.okhttp3:logging-interceptor:3.10.0" implementation "com.squareup.retrofit2:retrofit:2.4.0" implementation "com.squareup.retrofit2:converter-gson:2.4.0" implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-experimental-adapter:1.0.0' implementation 'com.google.code.gson:gson:2.8.2' implementation 'androidx.recyclerview:recyclerview:1.0.0-rc02' } kotlin { experimental { coroutines "enable" } }
  101. TUFQ $SFBUFDPNQPOFOUT

  102. class YourViewModel : ViewModel() { }

  103. class YourViewModel : ViewModel() { }

  104. class YourViewModel : ViewModel() { private val _repos = MutableLiveData<List<Repository>>()

    }
  105. class YourViewModel : ViewModel() { private val _repos = MutableLiveData<List<Repository>>()

    }
  106. class YourViewModel : ViewModel() { private val _repos = MutableLiveData<List<Repository>>()

    }
  107. class YourViewModel : ViewModel() { private val _repos = MutableLiveData<List<Repository>>()

    val repositories : LiveData<List<Repository>> }
  108. class YourViewModel : ViewModel() { private val _repos = MutableLiveData<List<Repository>>()

    val repositories : LiveData<List<Repository>> get() { if (_repos.value == null) { val repos = github.repositories() _repos.postValue(repos) } return _repos } }
  109. class YourViewModel : ViewModel() { private val _repos = MutableLiveData<List<Repository>>()

    val repositories : LiveData<List<Repository>> get() { if (_repos.value == null) { val repos = github.repositories() _repos.postValue(repos) } return _repos } }
  110. class YourViewModel : ViewModel() { private val _repos = MutableLiveData<List<Repository>>()

    val repositories : LiveData<List<Repository>> get() { if (_repos.value == null) { val repos = github.repositories() _repos.postValue(repos) } return _repos } }
  111. class YourViewModel : ViewModel() { private val _repos = MutableLiveData<List<Repository>>()

    val repositories : LiveData<List<Repository>> get() { if (_repos.value == null) { val repos = github.repositories() _repos.postValue(repos) } return _repos } }
  112. class YourViewModel : ViewModel() { private val _repos = MutableLiveData<List<Repository>>()

    val repositories : LiveData<List<Repository>> get() { if (_repos.value == null) { // Coroutine way launch(CommonPool) { val repos = github.repositories().await() _repos.postValue(repos) } } return _repos } }
  113. class YourViewModel : ViewModel() { private val _repos = MutableLiveData<List<Repository>>()

    val repositories : LiveData<List<Repository>> get() { if (_repos.value == null) { // Rx way github.repositories() .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe { repositories.postValue(it) } } return _repos } }
  114. TUFQ &OKPZZPVSDPNQPOFOUT

  115. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    ... val viewModel = ViewModelProviders.of(this)[YourViewModel::class.java] } }
  116. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    ... val viewModel = ViewModelProviders.of(this)[YourViewModel::class.java] viewModel.repositories.observe(this, Observer { // update Views }) } }
  117. $PODMVTJPO

  118. -JGFDZDMF.BOBHFNFOU -JGFDZDMFDPNQPOFOUT JTIBSE DBODBVTFQSPCMFNT IBTTJNQMFEFTJHO :PVDBOVTFFBTJMZ

  119. )JSPZVLJ.PSJ !NPSJESPJE 5IBOLZPV