zoukankan      html  css  js  c++  java
  • Rxlifecycle(一):使用

    Rxlifecycle使用非常方便简单,如下:

    1.集成

    build.gradle添加

    复制代码
       //Rxlifecycle
       compile 'com.trello:rxlifecycle:0.3.1'
       compile 'com.trello:rxlifecycle-components:0.3.1'
    
       //Rxjava
       compile 'io.reactivex:rxjava:1.0.16'
    复制代码

    Components包中包含RxActivity、RxFragment等等,可以用Rxlifecycle提供的,也可以自定义。

    2.Sample解析

    官方sample源码: 两种使用方法:

    1.手动设置取消订阅的时机,例子1、例子3

    2.绑定生命周期,自动取消订阅,例子2

    复制代码
    public class MainActivity extends RxAppCompatActivity {
    
    //Note:Activity需要继承RxAppCompatActivity,fragment需要继承RxFragment,等等
    //可以使用的组件在components包下面
    
    private static final String TAG = "RxLifecycle";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate()");
        setContentView(R.layout.activity_main);
    
        // Specifically bind this until onPause()
    
        //Note:例子1:
        Observable.interval(1, TimeUnit.SECONDS)
                .doOnUnsubscribe(new Action0() {
                    @Override
                    public void call() {
                        Log.i(TAG, "Unsubscribing subscription from onCreate()");
                    }
                })
                //Note:手动设置在activity onPause的时候取消订阅
                .compose(this.<Long>bindUntilEvent(ActivityEvent.PAUSE))
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long num) {
                        Log.i(TAG, "Started in onCreate(), running until onPause(): " + num);
                    }
                });
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart()");
    
        //Note:例子2:
        // Using automatic unsubscription, this should determine that the correct time to
        // unsubscribe is onStop (the opposite of onStart).
        Observable.interval(1, TimeUnit.SECONDS)
                .doOnUnsubscribe(new Action0() {
                    @Override
                    public void call() {
                        Log.i(TAG, "Unsubscribing subscription from onStart()");
                    }
                })
                //Note:bindToLifecycle的自动取消订阅示例,因为是在onStart的时候调用,所以在onStop的时候自动取消订阅
                .compose(this.<Long>bindToLifecycle())
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long num) {
                        Log.i(TAG, "Started in onStart(), running until in onStop(): " + num);
                    }
                });
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume()");
    
        //Note:例子3:
        // `this.<Long>` is necessary if you're compiling on JDK7 or below.
        // If you're using JDK8+, then you can safely remove it.
        Observable.interval(1, TimeUnit.SECONDS)
                .doOnUnsubscribe(new Action0() {
                    @Override
                    public void call() {
                        Log.i(TAG, "Unsubscribing subscription from onResume()");
                    }
                })
                //Note:手动设置在activity onDestroy的时候取消订阅
                .compose(this.<Long>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long num) {
                        Log.i(TAG, "Started in onResume(), running until in onDestroy(): " + num);
                    }
                });
    }
    ...
    复制代码
  • 相关阅读:
    微信小程序常用表单校验方法(手机号校验、身份证号(严格和非严格校验、验证码六位数字校验))
    css使Img图片居中显示
    formData请求接口传递参数格式
    台式机如何无线上网的解决方法
    (转译)2019年WEB漏洞扫描工具和软件前十名推荐
    如何往虚拟机内传文件的3种方法
    Python3.X Selenium 自动化测试中如何截图并保存成功
    postman如何绕过登录账户和密码验证,进行接口测试的方法
    官网Windows 10安装程序驱动下载--截止:2019.01.06版本
    postman教学视频百度网盘转载分享
  • 原文地址:https://www.cnblogs.com/ldq2016/p/8745012.html
Copyright © 2011-2022 走看看