Faced a decision that prompted an unexpected question for me. In his application (using retrofit'a 1.9) implemented the loading of a photo (from the gallery). From which the next idea emerged that the user can at any time simply cancel the download of this photo. Therefore, it was necessary to implement Cancel Request , given that in version 1.9 it is very painful . But again, the crux of the matter is not in this, it was decided to do something as follows, namely, to implement the CancelableCallback interface:
public abstract class CancelableCallback implements Callback<ImageUpload> { private static List<CancelableCallback> mList = new ArrayList<>(); private boolean isCanceled = false; private Object mTag = null; public static void cancelAll() { Iterator<CancelableCallback> iterator = mList.iterator(); while (iterator.hasNext()) { iterator.next().isCanceled = true; iterator.remove(); } } public static void cancel(Object tag) { if (tag != null) { Iterator<CancelableCallback> iterator = mList.iterator(); CancelableCallback item; while (iterator.hasNext()) { item = iterator.next(); if (tag.equals(item.mTag)) { item.isCanceled = true; iterator.remove(); } } } } public CancelableCallback() { mList.add(this); } public CancelableCallback(Object tag) { mTag = tag; mList.add(this); } public void cancel() { isCanceled = true; mList.remove(this); } @Override public final void success(ImageUpload t, Response response) { if (!isCanceled) onSuccess(t, response); mList.remove(this); } @Override public final void failure(RetrofitError error) { if (!isCanceled) onFailure(error); mList.remove(this); } public abstract void onSuccess(ImageUpload t, Response response); public abstract void onFailure(RetrofitError error); } After that, implement in our fragment:
CancelableCallback.cancel(TAG); And actually the question is, TAG how to handle it correctly and what are the ways to use it. So far I know that Tag is a kind of Object-storage for each View, where we can put the data you need ( I would like more detail ).