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

Inside Jetpack Architecture Components

kobito-kaba
September 26, 2018

Inside Jetpack Architecture Components

How LifecycleObservers, LiveData, ViewModels work

kobito-kaba

September 26, 2018
Tweet

More Decks by kobito-kaba

Other Decks in Programming

Transcript

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

    View Slide

  2. "7FOUVSFCFUXFFO4PGUCBOLBOE:BIPP
    0OFPGCJHHFTUTFBSDIJO+BQBO

    View Slide

  3. "SDIJUFDUVSF
    $PNQPOFOUT

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  7. -JGFDZDMF1SPCMFNT
    JO"OESPJE

    View Slide

  8. JOFBSMZEBZT

    View Slide

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

    View Slide

  10. DBTF
    UPVDIJOHEFTUSPZFE7JFX

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  19. DBTF
    GFUDIJOHBUFWFSZPO$SFBUF

    View Slide

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

    View Slide

  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
    
    
    
    
    

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  26. DBTF
    EJTQPTJOHJOXSPOHXBZ

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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
    
    
    
    
    
    
    

    View Slide

  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
    
    
    
    
    
    
    
    

    View Slide

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

    View Slide

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

    View Slide

  34. 4P XIBUXFTIPVE

    View Slide

  35. DBTFUPVDIJOHEFTUSPZFE7JFX
    DBTFGFUDIJOHBUFWFSZPO$SFBUF
    DBTFEJTQPTJOHJOXSPOHXBZ

    View Slide

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

    View Slide

  37. -JGFDZDMF0CTFSWFS

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  63. -JWF%BUB

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  78. 7JFX.PEFM

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  95. )PXUPVTF

    View Slide

  96. TUFQ
    "EEDPNQPOFOUT

    View Slide

  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

    View Slide

  98. dependencies {

    def lifecycle_version = "2.0.0-rc01"
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    }
    -JWF%BUB7JFX.PEFM

    View Slide

  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

    View Slide

  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"
    }
    }

    View Slide

  101. TUFQ
    $SFBUFDPNQPOFOUT

    View Slide

  102. class YourViewModel : ViewModel() {
    }

    View Slide

  103. class YourViewModel : ViewModel() {
    }

    View Slide

  104. class YourViewModel : ViewModel() {
    private val _repos = MutableLiveData>()
    }

    View Slide

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

    View Slide

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

    View Slide

  107. class YourViewModel : ViewModel() {
    private val _repos = MutableLiveData>()
    val repositories : LiveData>
    }

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  113. class YourViewModel : ViewModel() {
    private val _repos = MutableLiveData>()
    val repositories : LiveData>
    get() {
    if (_repos.value == null) {
    // Rx way
    github.repositories()
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeOn(Schedulers.io())
    .subscribe { repositories.postValue(it) }
    }
    return _repos
    }
    }

    View Slide

  114. TUFQ
    &OKPZZPVSDPNQPOFOUT

    View Slide

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

    View Slide

  116. class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
    ...
    val viewModel = ViewModelProviders.of(this)[YourViewModel::class.java]
    viewModel.repositories.observe(this, Observer { // update Views })
    }
    }

    View Slide

  117. $PODMVTJPO

    View Slide

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

    View Slide

  119. )JSPZVLJ.PSJ
    !NPSJESPJE
    5IBOLZPV

    View Slide