Cancel retrofit calls in fragment onDestroy

main
Eugen Rochko 8 years ago
parent 5e93e5d99c
commit 17b958f8ed
  1. 23
      app/src/main/java/com/keylesspalace/tusky/AccountFragment.java
  2. 11
      app/src/main/java/com/keylesspalace/tusky/NotificationsFragment.java
  3. 23
      app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java

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

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

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

Loading…
Cancel
Save