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

Android Architecture ComponentのLifecyle対応コンポーネントのおさらい / Review of Lifecyle aware components of AAC

Android Architecture ComponentのLifecyle対応コンポーネントのおさらい / Review of Lifecyle aware components of AAC

Ce6acc3536b0e0340b5f0569d3394c9c?s=128

Yasutaka Kawamoto

January 25, 2020
Tweet

Transcript

  1. Android Architecture Componentͷ LifecyleରԠίϯϙʔωϯτͷ ͓͞Β͍ Androidݚڀ&ൃදձ #4 2020/01/25

  2. • Տຊ ହ޹(͔Θ΋ͱ ΍͔ͨ͢) • ॴଐɿגࣜձࣾ tech vein 
 (େࡕࢢதԝ۠ຊொ)

    • ϞόΠϧΞϓϦΤϯδχΞ
 (AndroidଟΊɺiOS΋ŧŔŕŪũƄŝſ) • GitHub: kwmt ɺtwitter: kwmt27 • Google I/O2018 ॳࢀՃ • ෱Ԭग़਎ • ΫϥϑτϏʔϧ޷͖ ࣗݾ঺հ
  3. • Android Architecture Componentͱ͸ʁ • ViewModelͰ؆୯ը໘ճస • LiveDataΛ࢖ͬͯUIΛߋ৽ • LifecycleͰActivity΍FragmentΛεοΩϦ

    • ViewModelͷڞ༗ • Tips • ·ͱΊ ΞδΣϯμ
  4. Android Architecture Componentͱ͸ https://www.youtube.com/watch?v=LmkKFCfmnhQ&feature=youtu.be&hl=ja

  5. Android Architecture Componentͱ͸ https://www.youtube.com/watch?v=pErTyQpA390

  6. ViewModel, LiveData, Lifecycle ࠓ೔ͷओ໾

  7. ViewModelͰ؆୯ը໘ճస

  8. ՝୊ Activity ViewModelΛ࢖Θͣ ࣮૷ͨ͠৔߹ ը໘ճసͷͱ͖ɺ ʢͳʹ΋͠ͳ͍ͱʣ λΠϚʔ͕ΫϦΞ͞ΕΔ ※savedInstanceStateΛ࢖͑͹ ΫϦΞ͞Εͳ͍Α͏ʹ͢Δ͜ͱ͸Ͱ͖·͕͢ɺ ͜͜Ͱ͸ߟ͑·ͤΜɻ

    ྫɿλΠϚʔΞϓϦ
  9. ՝୊ Activity ViewModelΛ࢖Θͣ ࣮૷ͨ͠৔߹ ը໘ճసͷͱ͖ɺ ʢͳʹ΋͠ͳ͍ͱʣ λΠϚʔ͕ΫϦΞ͞ΕΔ ※savedInstanceStateΛ࢖͑͹ ΫϦΞ͞Εͳ͍Α͏ʹ͢Δ͜ͱ͸Ͱ͖·͕͢ɺ ͜͜Ͱ͸ߟ͑·ͤΜɻ

    ྫɿλΠϚʔΞϓϦ
  10. ViewModelΛ࢖͏ͱ Activity viewModel ը໘ճసͷͱ͖ɺ λΠϚʔ͕ΫϦΞ͞Εͳ͍Α͏ʹ ؆୯ʹͰ͖Δ ViewModel timer

  11. ViewModelΛ࢖͏ͱ Activity viewModel ը໘ճసͷͱ͖ɺ λΠϚʔ͕ΫϦΞ͞Εͳ͍Α͏ʹ ؆୯ʹͰ͖Δ ViewModel timer

  12. ViewModelͷੜଘظؒ ࣌ؒ Activity ViewModel

  13. ViewModelͷੜଘظؒ ࣌ؒ ىಈ Activity ViewModel

  14. ViewModelͷੜଘظؒ onCreate ࣌ؒ ViewModelੜ੒ onStart onResume ىಈ Activity ViewModel

  15. ViewModelͷੜଘظؒ onCreate ࣌ؒ ViewModelੜ੒ onStart onResume ىಈ ճస Activity ViewModel

  16. ViewModelͷੜଘظؒ onCreate ࣌ؒ ViewModelੜ੒ onStart onResume onPause onStop onDestroy onCreate

    onStart onResume ىಈ ճస Activity ViewModel
  17. ViewModelͷੜଘظؒ onCreate ࣌ؒ ViewModelੜ੒ onStart onResume onPause onStop onDestroy onCreate

    onStart onResume ىಈ ճస όοΫΩʔͰΞϓϦऴྃ Activity ViewModel
  18. ViewModelͷੜଘظؒ onCreate ࣌ؒ ViewModelੜ੒ onStart onResume onPause onStop onDestroy onCreate

    onStart onResume onPause onStop onDestroy ىಈ ճస όοΫΩʔͰΞϓϦऴྃ onCleared Activity ViewModel
  19. ViewModelͷੜଘظؒ onCreate ࣌ؒ ViewModelੜ੒ onStart onResume onPause onStop onDestroy onCreate

    onStart onResume onPause onStop onDestroy ىಈ ճస όοΫΩʔͰΞϓϦऴྃ onCleared Activity ViewModel
  20. override fun onCreate(savedInstanceState: Bundle?) { // লུ val chronometer =

    findViewById(R.id.chronometer) val startTime = viewModel.startTime if (startTime == null) { val time = SystemClock.elapsedRealtime() viewModel.startTime = time chronometer.base = time } else { chronometer.base = startTime } chronometer.start() } Activity class ChronoViewModel: ViewModel() { var startTime: Long? = null } ViewModel
  21. • Kotlin ϓϩύςΟσϦήʔτΛ࢖͏ํ๏ ViewModelΠϯελϯεͷऔಘํ๏ (஫ҙ)fragment-ktxͷ1.1.0Ҏ্Λ࢖͏ඞཁ͋Γ val viewModel by viewModels<MyViewModel>() https://developer.android.com/jetpack/androidx/releases/lifecycle#2.2.0-alpha03

    ViewModelProviders.of(this) .get(MyViewModel::class.java) • ViewModelProviderΛ࢖͏ํ๏ viewModel = ViewModelProvider(this) .get(MyViewModel::class.java)
  22. • Kotlin ϓϩύςΟσϦήʔτΛ࢖͏ํ๏ ViewModelΠϯελϯεͷऔಘํ๏ (஫ҙ)fragment-ktxͷ1.1.0Ҏ্Λ࢖͏ඞཁ͋Γ val viewModel by viewModels<MyViewModel>() https://developer.android.com/jetpack/androidx/releases/lifecycle#2.2.0-alpha03

    ViewModelProviders.of(this) .get(MyViewModel::class.java) • ViewModelProviderΛ࢖͏ํ๏ viewModel = ViewModelProvider(this) .get(MyViewModel::class.java) 2.2.0-alpha03͔ΒDeprecated
  23. • Activity΍FragmentͷϥΠϑαΠΫϧશମͰσʔλΛอ࣋ Ͱ͖Δ • Activity΍FragmentΑΓੜଘظ͕ؒ௕͍ΦϒδΣΫτ • ViewModelΫϥεͷ࡞੒͸ɺViewModel()Λܧঝ͢Δ͚ͩ • ViewModelΠϯελϯεΛऔಘ͢Δʹ͸ɺ
 KotlinϓϩύςΟσϦήʔτͱViewModelProviderΛ࢖͏

    ํ๏͕͋Δ ViewModel ·ͱΊ
  24. LiveDataΛ࢖ͬͯUIΛߋ৽

  25. • ઌఔͷλΠϚʔΞϓϦͰɺ
 UIΛ1ඵִؒͰ
 ʮʙඵܦաʯ
 ͱද͍ࣔͨ͠ • ͔ͭɺը໘ճస΋ߟྀ͍ͨ͠ ͜͜Ͱղܾ͍ͨ͠՝୊

  26. • ઌఔͷλΠϚʔΞϓϦͰɺ
 UIΛ1ඵִؒͰ
 ʮʙඵܦաʯ
 ͱද͍ࣔͨ͠ • ͔ͭɺը໘ճస΋ߟྀ͍ͨ͠ ͜͜Ͱղܾ͍ͨ͠՝୊

  27. ViewModelʹߋ৽͍ͨ͠ViewΛ ࣋ͨͤͨΒ͍͍Μ͡Όʁ class MyViewModel : ViewModel() { val initialTime =

    SystemClock.elapsedRealtime() private val timer = Timer() var timerTextView: TextView? = null init{ timer.scheduleAtFixedRate(object : TimerTask() { override fun run() { timerTextView?.text = "$ඵ਺ ඵܦա" } }, 1000, 1000) } }
  28. ViewModelʹߋ৽͍ͨ͠ViewΛ ࣋ͨͤͨΒ͍͍Μ͡Όʁ class MyViewModel : ViewModel() { val initialTime =

    SystemClock.elapsedRealtime() private val timer = Timer() var timerTextView: TextView? = null init{ timer.scheduleAtFixedRate(object : TimerTask() { override fun run() { timerTextView?.text = "$ඵ਺ ඵܦա" } }, 1000, 1000) } } • ΞϓϦऴྃ࣌ʹϝϞϦϦʔΫ͢ΔͷͰɺͰ͖·ͤΜɻ
  29. ViewModelʹߋ৽͍ͨ͠ViewΛ ࣋ͨͤͨΒ͍͍Μ͡Όʁ class MyViewModel : ViewModel() { val initialTime =

    SystemClock.elapsedRealtime() private val timer = Timer() var timerTextView: TextView? = null init{ timer.scheduleAtFixedRate(object : TimerTask() { override fun run() { timerTextView?.text = "$ඵ਺ ඵܦա" } }, 1000, 1000) } } • ΞϓϦऴྃ࣌ʹϝϞϦϦʔΫ͢ΔͷͰɺͰ͖·ͤΜɻ AACͷLiveDataΛ࢖͍·͢ʂ
  30. LiveDataΛ࢖͏ class MyViewModel : ViewModel() { val initialTime = SystemClock.elapsedRealtime()

    private val timer = Timer() private val elapsedTime = MutableLiveData<Long>() fun getElapsedTime(): LiveData<Long> = elapsedTime init{ timer.scheduleAtFixedRate(object : TimerTask() { override fun run() { elapsedTime.postValue(ඵ਺) } }, 1000, 1000) } } viewModel.getElapsedTime()
 .observe(this, Observer { timerTextView.text = "${t}ඵܦա" }) • Activityଆ
  31. LiveDataΛ࢖͏ class MyViewModel : ViewModel() { val initialTime = SystemClock.elapsedRealtime()

    private val timer = Timer() private val elapsedTime = MutableLiveData<Long>() fun getElapsedTime(): LiveData<Long> = elapsedTime init{ timer.scheduleAtFixedRate(object : TimerTask() { override fun run() { elapsedTime.postValue(ඵ਺) } }, 1000, 1000) } } viewModel.getElapsedTime()
 .observe(this, Observer { timerTextView.text = "${t}ඵܦա" }) • Activityଆ
  32. LiveDataΛ࢖͏ ViewModel Activity viewModel elapsedTime timerTextView.text

  33. LiveDataΛ࢖͏ ViewModel Activity viewModel elapsedTime observeͰ؂ࢹ timerTextView.text

  34. LiveDataΛ࢖͏ ViewModel Activity viewModel elapsedTime observeͰ؂ࢹ 1ඵ͝ͱʹߋ৽ timerTextView.text

  35. LiveDataΛ࢖͏ ViewModel Activity viewModel elapsedTime observeͰ؂ࢹ 1ඵ͝ͱʹߋ৽ timerTextView.text ௨஌

  36. • ViewModelʹViewΛ࣋ͨͤΔͷ͸ɺ
 ϝϞϦϦʔΫ͢ΔͷͰμϝ • ViewModelʹLiveDataΛ࣋ͨͤͯɺActivity΍Fragment͔ Β؂ࢹ(observe)͠ɺViewModel͔Β௨஌͢Δ LiveData ·ͱΊ

  37. LifecycleͰActivity΍FragmentΛεοΩϦ

  38. • ͨͱ͑͹ɺݱࡏ஍Λऔಘ͢ΔͨΊʹɺ LocationManager Λ࢖͏ͱ͠·͢ɻ
 ͜ͷΑ͏ͳAPIΛ࢖͏ͨΊʹ͸ɺ
 onResumeͰॳظԽɾsubscribeͨ͠Γ
 onPauseͰετοϓͨ͠Γɺunsubscribe͢Δ͜ͱ͕ඞཁ Ͱ͢ɻ ͜͜Ͱղܾ͍ͨ͠՝୊

  39. ͜͜Ͱղܾ͍ͨ͠՝୊ Activity override fun onResume(){ locationManager.requestLocationUpdates(লུ) } override fun onPause(){

    locationManager.removeUpdates(লུ) } • AndroidͷϥΠϒϥϦ΍API͸͜ͷΑ͏ͳॲཧΛ͢Δ΋ͷ ͕ଟ͍ͷͰɺActivity΍FragmentΫϥε͕๲ΒΜͰ͖ͯɺ ಡΈʹ͘͘ͳΓ·͢ɻ
  40. Activity override fun onCreate(){ LocationLifecyle(this) } Lifecycle class LocationLifecycle(lifecycleOwner: LifecycleOwner)

    ɹɹɹ: LifecycleObserver { init { lifecycleOwner.lifecycle.addObserver(this) } @OnLifecycleEvent(ON_RESUME) fun addLocationListener() { locationManager.requestLocationUpdates(লུ) } @OnLifecycleEvent(ON_PAUSE) fun removeLocationListener() { locationManager.removeUpdates(লུ) } }
  41. Activity override fun onCreate(){ LocationLifecyle(this) } Lifecycle class LocationLifecycle(lifecycleOwner: LifecycleOwner)

    ɹɹɹ: LifecycleObserver { init { lifecycleOwner.lifecycle.addObserver(this) } @OnLifecycleEvent(ON_RESUME) fun addLocationListener() { locationManager.requestLocationUpdates(লུ) } @OnLifecycleEvent(ON_PAUSE) fun removeLocationListener() { locationManager.removeUpdates(লུ) } } interfaceΛ࣮૷
  42. Activity override fun onCreate(){ LocationLifecyle(this) } Lifecycle class LocationLifecycle(lifecycleOwner: LifecycleOwner)

    ɹɹɹ: LifecycleObserver { init { lifecycleOwner.lifecycle.addObserver(this) } @OnLifecycleEvent(ON_RESUME) fun addLocationListener() { locationManager.requestLocationUpdates(লུ) } @OnLifecycleEvent(ON_PAUSE) fun removeLocationListener() { locationManager.removeUpdates(লུ) } } interfaceΛ࣮૷ observerΛొ࿥
  43. Activity override fun onCreate(){ LocationLifecyle(this) } Lifecycle class LocationLifecycle(lifecycleOwner: LifecycleOwner)

    ɹɹɹ: LifecycleObserver { init { lifecycleOwner.lifecycle.addObserver(this) } @OnLifecycleEvent(ON_RESUME) fun addLocationListener() { locationManager.requestLocationUpdates(লུ) } @OnLifecycleEvent(ON_PAUSE) fun removeLocationListener() { locationManager.removeUpdates(লུ) } } interfaceΛ࣮૷ observerΛొ࿥ ϥΠϑαΠΫϧʹґ ଘ͍ͨ͠ϝιουʹ ΞϊςʔγϣϯΛͭ ͚Δ
  44. • AndroidϥΠϑαΠΫϧʹؔΘΔAPI΍ϥΠϒϥϦΛ࢖͏ ࣌ʹɺAAC LifecycleΛ࢖͏ͱActivity΍FragmentҎ֎ͷ ΫϥεͰϥΠϑαΠΫϧݕ஌͕Ͱ͖Δɻ Lifecycle ·ͱΊ

  45. ViewModelͷڞ༗

  46. • Fragment͔ΒActivityʹͳʹ͔௨஌͍ͨͬͯ͠໘౗ͩͬͨ ΑͶʁ ͜͜Ͱղܾ͍ͨ͠՝୊

  47. class FugaFragment : Fragment() { interface OnClickListener { fun onClick()

    } private var onClickListener: OnClickListener? = null // লུ override fun onActivityCreated { // লུ onClickListener = requireActivity() as? OnClickListener // ϘλϯͳͲͷΫϦοΫΠϕϯτͰonClickΛݺͿ onClickListener?.onClick() } } class HogeActivity : AppCompatActivity(), FugaFragment.OnClickListener { // লུ override fun onClick() { // ͳʹ͔ } }
  48. class FugaFragment : Fragment() { interface OnClickListener { fun onClick()

    } private var onClickListener: OnClickListener? = null // লུ override fun onActivityCreated { // লུ onClickListener = requireActivity() as? OnClickListener // ϘλϯͳͲͷΫϦοΫΠϕϯτͰonClickΛݺͿ onClickListener?.onClick() } } class HogeActivity : AppCompatActivity(), FugaFragment.OnClickListener { // লུ override fun onClick() { // ͳʹ͔ } } ௨஌༻interfaceΛఆٛ ϑΟʔϧυʹม਺༻ҙ Activity͕interfaceΛ࣮૷ͯͨ͠ΒΩϟετ ௨஌༻ͷinterfaceΛ࣮૷
  49. • ActivityͱFragmentͰViewModelΛڞ༗ͯ͠ɺ
 ಉ͜͡ͱΛ΍ͬͯΈΔ ViewModelΛڞ༗

  50. class HogeActivity : AppCompatActivity() { private val viewModel by viewModels<HogeViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) { // লུ viewModel.clickEvent.observe(this) { textView.text = it } } } private val viewModel by viewModels<HogeViewModel> ({ requireActivity() }) viewModel.onClick() class HogeViewModel : ViewModel() { val clickEvent = MutableLiveData<String>() fun onClick() { clickEvent.value = "Ϙλϯ͕ԡ͞ΕͨΑ" } }
  51. ͜ΕΛಈ͔͢ͱ

  52. ͜ΕΛಈ͔͢ͱ

  53. • Fragment͔ΒActivity΁ͷ௨஌͕؆୯ • (͜͜Ͱ͸આ໌ͯ͠·ͤΜ͕)Fragmentಉ࢜ͷڞ༗΋Մೳ ViewModelڞ༗ͷ·ͱΊ

  54. ͜͜·Ͱ͸΄΅Codelabͷ಺༰ https://codelabs.developers.google.com/codelabs/android-lifecycles/#0

  55. Tips

  56. • 2ͭͷLiveDataΛ݁߹͍ͨ͠ • LiveDataͷൃՐΛҰ౓͚ͩʹ͍ͨ͠ Tips

  57. • 2ͭͷLiveDataΛ݁߹͍ͨ͠ • LiveDataͷൃՐΛҰ౓͚ͩʹ͍ͨ͠ Tips

  58. 2ͭͷLiveDataΛ݁߹͍ͨ͠

  59. MediatorLiveDataΛ࢖͏ class ZipLIveDataViewModel : ViewModel() { val editText1 = MutableLiveData<String>()

    val editText2 = MutableLiveData<String>() val isEnabled = MediatorLiveData<Boolean>() init { val observer = Observer<String> { isEnabled.value = editText1.value?.isNotEmpty() ?: false && editText2.value?.isNotEmpty() ?: false } isEnabled.addSource(editText1, observer) isEnabled.addSource(editText2, observer) } }
  60. MediatorLiveDataͷϥούʔؔ਺Λ࡞Δ fun <A, B> zipLiveData(a: LiveData<A>, b: LiveData<B>): LiveData<Pair<A, B>>

    { return MediatorLiveData<Pair<A, B>>().apply { var lastA: A? = null var lastB: B? = null fun update() { val localLastA = lastA val localLastB = lastB if (localLastA != null && localLastB != null) { this.value = Pair(localLastA, localLastB) } } addSource(a) { lastA = it update() } addSource(b) { lastB = it update() } } } https://medium.com/@gauravgyal/combine-results-from-multiple-async-requests-90b6b45978f7
  61. MediatorLiveDataͷϥούʔؔ਺Λ࢖͏ class ZipLIveDataViewModel : ViewModel() { val editText1 = MutableLiveData<String>()

    val editText2 = MutableLiveData<String>() val isEnabled: LiveData<Boolean> = zipLiveData(editText1, editText2) .map { (t1, t2) -> t1.isNotEmpty() && t2.isNotEmpty() } }
  62. ൺ΂Δ class ZipLIveDataViewModel : ViewModel() { val editText1 = MutableLiveData<String>()

    val editText2 = MutableLiveData<String>() val isEnabled: LiveData<Boolean> = zipLiveData(editText1, editText2) .map { (t1, t2) -> t1.isNotEmpty() && t2.isNotEmpty() } } class ZipLIveDataViewModel : ViewModel() { val editText1 = MutableLiveData<String>() val editText2 = MutableLiveData<String>() val isEnabled = MediatorLiveData<Boolean>() init { val observer = Observer<String> { isEnabled.value = editText1.value?.isNotEmpty() ?: false && editText2.value?.isNotEmpty() ?: false } isEnabled.addSource(editText1, observer) isEnabled.addSource(editText2, observer) } }
  63. ಈ͔͢

  64. ಈ͔͢

  65. • 2ͭͷLiveDataΛ݁߹͍ͨ͠ • LiveDataͷൃՐΛҰ౓͚ͩʹ͍ͨ͠ Tips

  66. • ը໘ભҠʹLiveDataΛ࢖͏ͱɺ໭Δ͜ͱ͕Ͱ͖ͳ͍ LiveDataͷൃՐΛҰ౓͚ͩʹ͍ͨ͠

  67. ભҠݩʹ໭Εͳ͍࣮૷ྫ override fun onViewCreated() { viewModel.nextLiveData .observe(viewLifecycleOwner) { nextViewType ->

    // ը໘ભҠ͢Δ } button .setOnClickListener{viewModel.onClickNextButton()}
 } fun onClickNextButton() { // APIϨεϙϯεͷ݁ՌʹΑͬͯɺը໘ભҠΛม͍͑ͨ৔߹ val data = repository.fetchData() // লུ _nextLiveData.value = nextType } Fragment ViewModel
  68. ભҠݩʹ໭Εͳ͍࣮૷ྫ override fun onViewCreated() { viewModel.nextLiveData .observe(viewLifecycleOwner) { nextViewType ->

    // ը໘ભҠ͢Δ } button .setOnClickListener{viewModel.onClickNextButton()}
 } fun onClickNextButton() { // APIϨεϙϯεͷ݁ՌʹΑͬͯɺը໘ભҠΛม͍͑ͨ৔߹ val data = repository.fetchData() // লུ _nextLiveData.value = nextType } Fragment ViewModel όοΫͰ໭͖ͬͯͨͱ͖ʹɺ onViewCreated͕ݺ͹Εͯɺ observe͍ͯ͠ΔonChanged͕ݺ͹ΕΔͨΊ
  69. LiveDataͷϥούʔΫϥεΛ࡞Δ https://medium.com/androiddevelopers/livedata-with-snackbar-navigation-and-other-events-the-singleliveevent-case-ac2622673150 data class Event<out T>(private val content: T) {

    var hasBeenHandled = false private set fun getContentIfNotHandled(): T? { return if (hasBeenHandled) { null } else { hasBeenHandled = true content } } }
  70. LiveDataͷϥούʔΫϥε(Event)Λ࢖͏ override fun onViewCreated() { viewModel.nextLiveData .observe(viewLifecycleOwner) { event ->

    event.getContentIfNotHandled()?.let {nextViewType -> // ը໘ભҠ͢Δ
 } } button .setOnClickListener{viewModel.onClickNextButton()}
 } fun onClickNextButton() { // APIϨεϙϯεͷ݁ՌʹΑͬͯɺը໘ભҠΛม͍͑ͨ৔߹ val data = repository.fetchData() // লུ _nextLiveData.value = Event(nextType) } Fragment ViewModel
  71. LiveDataͷϥούʔΫϥε(Event)Λ࢖͏ override fun onViewCreated() { viewModel.nextLiveData .observe(viewLifecycleOwner) { event ->

    event.getContentIfNotHandled()?.let {nextViewType -> // ը໘ભҠ͢Δ
 } } button .setOnClickListener{viewModel.onClickNextButton()}
 } fun onClickNextButton() { // APIϨεϙϯεͷ݁ՌʹΑͬͯɺը໘ભҠΛม͍͑ͨ৔߹ val data = repository.fetchData() // লུ _nextLiveData.value = Event(nextType) } Fragment ViewModel EventΛηοτ͢ΔΑ͏ʹมߋ
  72. LiveDataͷϥούʔΫϥε(Event)Λ࢖͏ override fun onViewCreated() { viewModel.nextLiveData .observe(viewLifecycleOwner) { event ->

    event.getContentIfNotHandled()?.let {nextViewType -> // ը໘ભҠ͢Δ
 } } button .setOnClickListener{viewModel.onClickNextButton()}
 } fun onClickNextButton() { // APIϨεϙϯεͷ݁ՌʹΑͬͯɺը໘ભҠΛม͍͑ͨ৔߹ val data = repository.fetchData() // লུ _nextLiveData.value = Event(nextType) } Fragment ViewModel getContentIfNotHandled()ͷ࢓૊ΈͰɺ όοΫͰ໭͖ͬͯͨͱ͖͸null͕ฦΔ ͨΊɺը໘ભҠ͠ͳ͘ͳΓɺ ݩͷը໘ʹ໭Δ͜ͱ͕Ͱ͖Δ EventΛηοτ͢ΔΑ͏ʹมߋ
  73. ಈ͔͢

  74. ಈ͔͢

  75. • Activity΍Fragment͕ը໘ճసͳͲͰ࠶ੜ੒͞Εͯ΋อ͓͖࣋ͯͨ͠ ͍৘ใΛViewModelʹอ࣋͢Δͱ޾ͤʹͳΔ • ViewModelͰܭࢉͨ͠Γอ࣋ͨ͠σʔλͰUIΛߋ৽͢Δʹ͸ɺ LiveDataΛ࢖ͬͯUIʹ௨஌͢Δɻ • AACͷLifecycle͸Activity΍FragmentͷϥΠϑαΠΫϧͰॳظԽ΍ഁغ Λ͍ͯͨ͠ίϯϙʔωϯτΛɺผΫϥεʹ੾Γग़ͤͯεοΩϦͰ͖Δ •

    ViewModelΛڞ༗͢Δ͜ͱͰɺFragment−ActivityؒΛָʹڞ༗Ͱ͖Δ • ը໘ભҠͰLiveDataΛ࢖͏ͱ͖͸ɺϥούʔΫϥεΛ࡞͓ͬͯ͘ඞཁ ͕͋Γͦ͏ ·ͱΊ
  76. • ΋ͬͱৄ͍͠࢓૊ΈΛϒϩάʹ·ͱΊͯ·͢ͷͰɺ͝ ڵຯ͕͋Δํ͸ݟͯΈ͍ͯͩ͘͞ɻ ࠷ޙʹ https://kwmt27.net/2019/09/19/android-lifecycle-codelab/