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);
}
});
}
...

最新文章

  1. iOS_直播类app_HTTP Live Streaming
  2. Bool 类型变量的使用
  3. AR播放器
  4. iOS 原生网络请求(推荐使用AFNetWorking库)
  5. 【原创】OPA857 TEST模式使用
  6. java版本的学生管理系统
  7. Ajax学习(1)-简单ajax案例
  8. MVC构架思想
  9. Secure CRT 如何连接虚拟机里面的CentOS系统——当主机使用无线网的时候 作者原创 欢迎转载
  10. Android 将从网络获取的数据缓存到私有文件
  11. 自行扩展 FineUIMvc 通知对话框(多个并排显示不重叠,支持最新的显示在最上方)
  12. NIO Socket编程实例
  13. 【java】文件复制的简单实现
  14. js 监听手机端键盘弹出和收起事件
  15. Codeforces 1068 - A/B/C/D/E - (Done)
  16. Flex布局新写法兼容写法详解
  17. CentOS7安装配置Amanda
  18. 闹钟AlarmAndMusic 和支持播放音乐效果《IT蓝豹》
  19. TCP/IP协议(3):数据链路层
  20. jquery ajax 的 $.get()用法详解

热门文章

  1. haskell io模块
  2. linux下卸载mysql
  3. 我们一起学SASS
  4. SSRS 迁移
  5. jenkins2 groovy语法
  6. 专题:点滴Javascript
  7. servlet--转向forward与重定向
  8. chosen PersistenceUnitInfo does not specify a provider class name
  9. CSS3 圆角制作的消息提示图标
  10. iOS开发-UIScrollView原理