Introduction to Android livedata (i)
With Android Livedata, you need to add a reference to the Gradle:
Compile "android.arch.lifecycle:runtime:1.0.0" compile "android.arch.lifecycle:extensions:1.0.0 " Annotationprocessor "android.arch.lifecycle:compiler:1.0.0"
This article is based on three references to the new version 1.0.0 write code, I use other versions such as version 1.0.0alpha4, the code run error. If this is the case, please add the latest version reference yourself. In 1.0.0, individual classes and methods have been deprecated or adjusted by Google Android, for example, lifecycleactivity is obsolete, Android has officially recommended that developers use appcompatactivity to replace Lifecycleactivity. Some of the content and technology about lifecycle have been written to appcompatactivity by Android officials.
Livedata implements basic Android Activity/fragment life-cycle awareness, which itself holds observable data, and developers can decouple/decouple from the Android lifecycle with Livedata Onactive/oninactive implementations, Through the Postvalue or SetValue method of Livedata, the Observer is informed of the change of the observer data and the observable change data is transmitted through observer onchanged.
(a) The use of Livedata, the first establishment of livedata data, generally inherited from Mutablelivedata.
Mutablelivedata is a subclass of Livedata, with the addition of public methods SetValue and Postvalue, which are easy for developers to use directly. SetValue must be called on the main thread. Postvalue can be called in a background thread.
This article writes a simple MyData inherited from Mutablelivedata. The MyData internally implements a simple function that runs a long-running thread task in the background, which implements a simple function:
(a) If the current activity is in a running (user-visible) state, the thread task constantly accumulates the counter and postvalue a value for any observer use.
(b) If the current activity is inactive, pause the thread task and stop accumulating the counter.
(a) (b) Two functions are completed by one thread task, see appendix article 1 for specific implementations.
In Livedata, the Onactive method callback indicates that the activity is active in the active state (Onstart,onresume), and it is simple to assume that the current activity is in the foreground.
Livedata's oninactive processing involves onactive the remainder of the life cycle and can be easily understood Oninactive is Android Activity/fragment inactive (backstage, For example, the current activity is in the life cycle of onstop,onpause). Mydata.java:
Package Zhangphil.app;import Android.arch.lifecycle.mutablelivedata;import Android.util.log;public class MyData Extends Mutablelivedata<string> {private final String TAG = "Livedata"; private int count = 0; Private Boolean RUN = true; Private Longtimework Mthread = new Longtimework (); Public MyData () {Mthread.start (); } @Override protected void Onactive () {super.onactive (); LOG.D (TAG, "onactive"); RUN = true; Mthread.interrupt (); } @Override protected void Oninactive () {super.oninactive (); LOG.D (TAG, "oninactive"); RUN = false; } private class Longtimework extends Thread {@Override public void run () {while (true) { try {if (! RUN) {thread.sleep (long.max_value); }} catch (Exception e) {e.printstacktrace (); } count++; Postvalue (String.valueof (count)); try {thread.sleep (3000); } catch (Interruptedexception e) {e.printstacktrace (); } } } }}
(ii) Building observer, waiting for change data to be transmitted in the onchanged of observer.
Data changes in Livedata, after postvalue (background thread) or SetValue (main thread) settings, will trigger observer onchanged, developers only need to onchanged wait for the latest data callback.
Package Zhangphil.app;import Android.arch.lifecycle.observer;import Android.support.annotation.nullable;import Android.os.bundle;import Android.support.v7.app.appcompatactivity;import Android.widget.toast;public Class Mainactivity extends Appcompatactivity { @Override protected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Myobserver observer = new Myobserver (); MyData data = new MyData (); Data.observe (this, observer); } Private class Myobserver implements observer<string> { @Override public void onChanged (@Nullable String o { Toast.maketext (Getapplicationcontext (), string.valueof (o), Toast.length_short). Show ();} }
Summary: Android's livedata is particularly handy for handling life-cycle data related to the Android lifecycle. Imagine, a task, if the activity is not visible, such as when the user presses the home key is not visible, at this time may not want to do the processing, then you can do a logical processing in the Livedata oninactive, change the state of the thread task, If the current activity is returned to the foreground by the user, the task can be resumed at Livedata Onactive.
Appendix:
1, "Android uses thread's interrupt with sleep, restart or pause threads task" link: http://blog.csdn.net/zhangphil/article/details/78584136
Introduction to Android livedata (i)