Cancel retrofit calls in fragment onDestroy

This commit is contained in:
Eugen Rochko 2017-03-14 12:45:19 +01:00
parent 5e93e5d99c
commit 17b958f8ed
3 changed files with 48 additions and 9 deletions

View file

@ -40,10 +40,13 @@ import retrofit2.Callback;
public class AccountFragment extends Fragment implements AccountActionListener {
private static final String TAG = "Account"; // logging tag
private Call<List<Account>> listCall;
public enum Type {
FOLLOWS,
FOLLOWERS,
BLOCKS,
MUTES,
}
private Type type;
@ -141,6 +144,12 @@ public class AccountFragment extends Fragment implements AccountActionListener {
return rootView;
}
@Override
public void onDestroy() {
super.onDestroy();
if (listCall != null) listCall.cancel();
}
@Override
public void onDestroyView() {
if (jumpToTopAllowed()) {
@ -166,15 +175,23 @@ public class AccountFragment extends Fragment implements AccountActionListener {
switch (type) {
default:
case FOLLOWS: {
api.accountFollowing(accountId, fromId, uptoId, null).enqueue(cb);
listCall = api.accountFollowing(accountId, fromId, uptoId, null);
listCall.enqueue(cb);
break;
}
case FOLLOWERS: {
api.accountFollowers(accountId, fromId, uptoId, null).enqueue(cb);
listCall = api.accountFollowers(accountId, fromId, uptoId, null);
listCall.enqueue(cb);
break;
}
case BLOCKS: {
api.blocks(fromId, uptoId, null).enqueue(cb);
listCall = api.blocks(fromId, uptoId, null);
listCall.enqueue(cb);
break;
}
case MUTES: {
listCall = api.mutes(fromId, uptoId, null);
listCall.enqueue(cb);
break;
}
}

View file

@ -48,6 +48,7 @@ public class NotificationsFragment extends SFragment implements
private EndlessOnScrollListener scrollListener;
private NotificationsAdapter adapter;
private TabLayout.OnTabSelectedListener onTabSelectedListener;
private Call<List<Notification>> listCall;
public static NotificationsFragment newInstance() {
NotificationsFragment fragment = new NotificationsFragment();
@ -122,6 +123,12 @@ public class NotificationsFragment extends SFragment implements
sendFetchNotificationsRequest();
}
@Override
public void onDestroy() {
super.onDestroy();
if (listCall != null) listCall.cancel();
}
@Override
public void onDestroyView() {
TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tab_layout);
@ -137,7 +144,9 @@ public class NotificationsFragment extends SFragment implements
private void sendFetchNotificationsRequest(final String fromId, String uptoId) {
MastodonAPI api = ((BaseActivity) getActivity()).mastodonAPI;
api.notifications(fromId, uptoId, null).enqueue(new Callback<List<Notification>>() {
listCall = api.notifications(fromId, uptoId, null);
listCall.enqueue(new Callback<List<Notification>>() {
@Override
public void onResponse(Call<List<Notification>> call, retrofit2.Response<List<Notification>> response) {
if (response.isSuccessful()) {

View file

@ -39,6 +39,8 @@ public class TimelineFragment extends SFragment implements
SwipeRefreshLayout.OnRefreshListener, StatusActionListener {
private static final String TAG = "Timeline"; // logging tag
private Call<List<Status>> listCall;
enum Kind {
HOME,
PUBLIC,
@ -144,6 +146,12 @@ public class TimelineFragment extends SFragment implements
sendFetchTimelineRequest();
}
@Override
public void onDestroy() {
super.onDestroy();
if (listCall != null) listCall.cancel();
}
@Override
public void onDestroyView() {
if (jumpToTopAllowed()) {
@ -184,23 +192,28 @@ public class TimelineFragment extends SFragment implements
switch (kind) {
default:
case HOME: {
api.homeTimeline(fromId, uptoId, null).enqueue(cb);
listCall = api.homeTimeline(fromId, uptoId, null);
listCall.enqueue(cb);
break;
}
case PUBLIC: {
api.publicTimeline(null, fromId, uptoId, null).enqueue(cb);
listCall = api.publicTimeline(null, fromId, uptoId, null);
listCall.enqueue(cb);
break;
}
case TAG: {
api.hashtagTimeline(hashtagOrId, null, fromId, uptoId, null).enqueue(cb);
listCall = api.hashtagTimeline(hashtagOrId, null, fromId, uptoId, null);
listCall.enqueue(cb);
break;
}
case USER: {
api.accountStatuses(hashtagOrId, fromId, uptoId, null).enqueue(cb);
listCall = api.accountStatuses(hashtagOrId, fromId, uptoId, null);
listCall.enqueue(cb);
break;
}
case FAVOURITES: {
api.favourites(fromId, uptoId, null).enqueue(cb);
listCall = api.favourites(fromId, uptoId, null);
listCall.enqueue(cb);
break;
}
}