Android architecture components -Lifecycle, LiveData, ViewModel

Lifecycle

Lifecycle components include LifecycleOwner, LifecleObserver, can easily monitor Activity or Fragment life cycle.

step:

1. To achieve LifecycleObserver Interface

  • DefaultLifecycleObserver implement the interface, then the method of rewriting life cycle inside;
  • LifecycleObserver directly implement the interface, then receives the life cycle changes by way of annotation;

For both forms, Lifecycle.java document is recommended to use the first approach, because the document is described, along with Java8 become mainstream, annotation approach will be abandoned.

2. Add the observer.

   By getLifeCycle ( ) . The addObserver (mPresenter ) Method

 Activity base class implements LifecycleOwner method, it is possible to obtain the direct method.

3. monitor the life cycle.

Note: When activity / Fragment life cycle changes, onStart, after onResume method calls invoke Observer override method, onPause / onDestory vice versa.

 

LiveData

LiveData is a data holding class, has the following characteristics:

1. Data can be subscribed viewers.

2. Can aware components (Fragment, Activity, Service) life cycle.

2. Only the component is active (STATED, RESUME) will inform the data refresh. (Except state DESTORY onDestory method, other methods are all callbacks STARTD / RESUME etc.)

LiveData advantages:

  • To ensure that data and unified UI

 And using the observer pattern about LiveData, LiveData observer is, when there is change data informs the viewer (UI).

  • Reducing memory leaks

 This is because LiveData can sense the life cycle of components, when the assembly is DESTROYED state, the observer object will be removed.

  • When the stop will not cause the collapse of Activity

 This is because when the assembly is in an inactive state, will not be notified in LiveData data changes.

  • No additional manual processing in response to changes in the life cycle

 This is also because LiveData can sense the life cycle of components, so we do not need to tell the complete lifecycle state LiveData components in the code.

  • Components and data related to the content can be updated in real time

 Assembly at the front desk to receive real-time notification when data changes, it is understandable. When a component from the background to the foreground, LiveData can notice the latest data components, these two components will ensure the content and related data can be updated in real time.

  • When for configuration change, no additional processing to save data

  We know that when you put the data stored in the assembly, when the configuration change (such as language, screen orientation change), the component is recreate, but the system does not guarantee that your data can be recovered. When we use LiveData save the data, because the data and the components separated. When a component is recreate, the data still exists in LiveData, and will not be destroyed.

  • Resource Sharing

 LiveData through inheritance class, then the class definition into a single embodiment mode, some systems monitor changes in properties such package, and then notifies LiveData observer will see this in the specific example LiveData in succession.

LiveData use:

Internal LiveData is achieved by listening callback Lifecycle, concrete can see the source code.

step:

1. Create a stored LiveData instance of a particular data type;

2. Create Observer object as a parameter LiveData add an observer;

3. Update LiveData objects stored data.

 

1. Add dependence

//viewModel
    api 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha02'
//LifecycleObserver 与 LiveData
    api 'androidx.lifecycle:lifecycle-extensions:2.2.0-alpha02'

 

2. Create an instance of LiveData

 Android document recommends that LiveData ViewModel meet up with the use, of course, it can also be used alone, but need to pay attention to the separation between the data and components, LiveData object components must not hold, or else when the component is destroyed, LiveData still holds Activity / Fragment objects, resulting in a component can not be recovered.

     MutableLiveData as LiveData subclasses.

 3. Create Observer objects, add an observer.

Created by adding observer LiveData.Observe () method in the component, it will inform the viewer through the callback method when the data changes, the pop-up toast.

Add observer for the two kinds of ways:

  • observe () method: When the component lifecycle changes, onStateChanged () method is called. When the component is DESTORY state, it will be automatically unsubscribed.
  • observeForever () method: onStateChanged () method is called three times (CREATED, STARTED, RESUMED), you will not receive state DESTROYED behind the need to manually unsubscribe.

4. Update data in LiveData.

Updated LiveData There are two ways:

  • setValue (): the main thread calls, if the non-main thread will throw an exception.
  • postValue (): both can also be called in the child thread in the main thread will eventually call the setValue method to change the stored data.

 

 Reference links:

1.https://blog.csdn.net/zhuzp_blog/article/details/78871374

2.https://blog.csdn.net/zhuzp_blog/article/details/78871527

Guess you like

Origin www.cnblogs.com/fangg/p/11284575.html