I'm using RxJava2 and Retrofit. In my fragment, I make a request to upload a local file:
Disposable disposable = mApi.requestUpload(file)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(response -> {
toast("success");
}, throwable -> {
toast("failed");
});
mCompositeDisposable.add(disposable);
Then, clear all disposables in onDestroyView()
@Override
public void onDestroyView() {
mCompositeDisposable.clear();
super.onDestroyView();
}
But I use Charles to view all requests and find that the request is still executing after I finish the fragment. The file is still uploaded successfully after a time.
How can I cancel the call when fragment closed?