-
-
Notifications
You must be signed in to change notification settings - Fork 85
请求生命周期绑定
xuexiangjys edited this page Mar 16, 2019
·
1 revision
在请求时,订阅ProgressLoadingSubscriber
或者ProgressLoadingCallBack
,传入请求消息加载者IProgressLoader
,即可完成生命周期的绑定。示例如下:
XHttpRequest req = ApiProvider.getAddUserReq(getRandomUser());
XHttpSDK.executeToMain(req, new ProgressLoadingSubscriber<Boolean>(mIProgressLoader) {
@Override
public void onSuccess(Boolean aBoolean) {
ToastUtils.toast("用户添加成功!");
mRefreshLayout.autoRefresh();
}
});
(1)这里需要依赖一下RxUtil2
implementation 'com.github.xuexiangjys:rxutil2:1.1.2'
(2)在所在的Activity的onCreate()下锁定Activity.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RxLifecycle.injectRxLifecycle(this);
}
(3)然后在请求中使用RxJava的compose
的操作符进行绑定。
.compose(RxLifecycle.with(this).<Boolean>bindToLifecycle())