From 17b958f8ed61b284310d05df63f42f7a38b00d4b Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 14 Mar 2017 12:45:19 +0100 Subject: [PATCH] Cancel retrofit calls in fragment onDestroy --- .../keylesspalace/tusky/AccountFragment.java | 23 ++++++++++++++++--- .../tusky/NotificationsFragment.java | 11 ++++++++- .../keylesspalace/tusky/TimelineFragment.java | 23 +++++++++++++++---- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/AccountFragment.java b/app/src/main/java/com/keylesspalace/tusky/AccountFragment.java index e754abcb..f5044783 100644 --- a/app/src/main/java/com/keylesspalace/tusky/AccountFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/AccountFragment.java @@ -40,10 +40,13 @@ import retrofit2.Callback; public class AccountFragment extends Fragment implements AccountActionListener { private static final String TAG = "Account"; // logging tag + private Call> 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; } } diff --git a/app/src/main/java/com/keylesspalace/tusky/NotificationsFragment.java b/app/src/main/java/com/keylesspalace/tusky/NotificationsFragment.java index dacebb1e..b0cbe28b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/NotificationsFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/NotificationsFragment.java @@ -48,6 +48,7 @@ public class NotificationsFragment extends SFragment implements private EndlessOnScrollListener scrollListener; private NotificationsAdapter adapter; private TabLayout.OnTabSelectedListener onTabSelectedListener; + private Call> 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>() { + listCall = api.notifications(fromId, uptoId, null); + + listCall.enqueue(new Callback>() { @Override public void onResponse(Call> call, retrofit2.Response> response) { if (response.isSuccessful()) { diff --git a/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java b/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java index 4037dbc4..fff484cb 100644 --- a/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java @@ -39,6 +39,8 @@ public class TimelineFragment extends SFragment implements SwipeRefreshLayout.OnRefreshListener, StatusActionListener { private static final String TAG = "Timeline"; // logging tag + private Call> 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; } }