Cov txheej txheem:

Puas yog ViewModel Life Cycle Paub?
Puas yog ViewModel Life Cycle Paub?

Video: Puas yog ViewModel Life Cycle Paub?

Video: Puas yog ViewModel Life Cycle Paub?
Video: KotaTalks 9: Cara Menjadi Seorang Mobile Developer 2024, Kaum ib hlis
Anonim

ViewModel cov khoom tuaj yeem muaj LifecycleObservers, xws li LiveData cov khoom, tab sis a ViewModel yuav tsum tsis txhob saib cov kev hloov pauv rau lub neej voj voog - paub Observables, qhov no yuav tsum tau ua ntawm LifecycleOwner.

Tsis tas li ntawd, koj yuav ua li cas thiaj paub txog ViewModel lifecycle?

Lifecycle-Aware Components

  1. Taw qhia.
  2. Kauj Ruam 1 - Teeb Koj Ib puag ncig.
  3. Kauj ruam 2 - Ntxiv ViewModel.
  4. Kauj ruam 3 - qhwv cov ntaub ntawv siv LiveData.
  5. Kauj Ruam 4 - Sau npe rau Lifecycle Events.
  6. Kauj ruam 5 - Qhia ib ViewModel ntawm Fragments.
  7. Kauj Ruam 6 - Persist ViewModel xeev hla cov txheej txheem ua si (beta)

Tsis tas li ntawd, ViewModel Android yog dab tsi? Published on May 29, 2018. ViewModel yog ib feem ntawm lub tsev qiv ntawv Lifecycle uas tau tsim los pab koj daws teeb meem Android Cov kev sib tw Lifecycle thiab ua rau koj cov apps tswj tau thiab kuaj tau. A ViewModel tuav koj lub app cov ntaub ntawv UI nyob rau hauv txoj kev paub txog lub neej uas muaj sia nyob cov kev hloov pauv.

Kuj paub yog, LifecycleOwner yog dab tsi?

LifecycleOwner . LifecycleOwner yog ib txoj kev sib cuam tshuam uas qhia tias chav kawm muaj Lifecycle. Nws muaj ib txoj hauv kev, getLifecycle(), uas yuav tsum tau ua los ntawm chav kawm.

ViewModel tuaj yeem sib txuas lus nrog cov khoom tawg thiab kev ua haujlwm li cas?

Tso cai rau ib Fragment rau sib txuas lus mus txog nws Kev ua si , koj tuaj yeem txhais qhov interface hauv qhov Fragment chav kawm thiab siv nws nyob rau hauv Kev ua si . Cov Fragment captures qhov kev siv interface thaum lub sij hawm nws onAttach() lifecycle txoj kev thiab ces hu rau lub Interface txoj kev thiaj li yuav sib txuas lus nrog rau Kev ua si.

Pom zoo: