Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

希望能像volley一样可以取消某个链接 #3

Closed
yhsj0919 opened this issue Nov 3, 2015 · 4 comments
Closed

希望能像volley一样可以取消某个链接 #3

yhsj0919 opened this issue Nov 3, 2015 · 4 comments

Comments

@yhsj0919
Copy link

yhsj0919 commented Nov 3, 2015

很多时候网络请求会引起空指针等异常,希望能在关闭activity的时候同时取消当前页面的请求

@wyouflf
Copy link
Owner

wyouflf commented Nov 6, 2015

添加自定义Callback, 由Callback决定是否取消链接:

public abstract class BindingPageCallback
implements Callback.CommonCallback, Callback.Cancelable {

    private boolean cancelled = false;
    private final WeakReference<Object> pageRef;

    public BindingPageCallback(Object page) {
        this.pageRef = new WeakReference<Object>(page);
    }

    @Override
    public void cancel() {
        cancelled = true;
    }

    @Override
    public boolean isCancelled() {
        if (cancelled) {
            return true;
        } else {
            Object page = pageRef.get();
            if (page == null) {
                return true;
            }

            if (page instanceof Activity) {
                return ((Activity) page).isFinishing();
            } else if (page instanceof Fragment/* v4? */) {
                return ((Fragment) page).isDetached();
            } else {
                .....
            }
        }

        return false;
    }
}

@wyouflf wyouflf closed this as completed Nov 6, 2015
@Kerwin92
Copy link

@wyouflf ,您好,这个抽象类在哪儿继承使用呢?自己没想出来,求指教

@kumaleap kumaleap mentioned this issue May 27, 2016
@TuWei1992
Copy link

@wyouflf 我想在onError的时候取消这个request应该怎么处理?

@yuyongxue
Copy link

学习了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants