From 67f90d050c0a5775f852f927967a09f15a46b307 Mon Sep 17 00:00:00 2001 From: Conny Duck Date: Sun, 8 Jul 2018 10:16:19 +0200 Subject: [PATCH] fix crash in TimelineFragment --- .../tusky/fragment/TimelineFragment.java | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java index 2755522b7..f6ff4d66b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java @@ -206,6 +206,9 @@ public class TimelineFragment extends SFragment implements || kind == Kind.LIST) { hashtagOrId = arguments.getString(HASHTAG_OR_ID_ARG); } + + adapter = new TimelineAdapter(dataSource, this); + } @Override @@ -218,9 +221,6 @@ public class TimelineFragment extends SFragment implements progressBar = rootView.findViewById(R.id.progress_bar); nothingMessageView = rootView.findViewById(R.id.nothing_message); - adapter = new TimelineAdapter(dataSource, this); - - setupSwipeRefreshLayout(); setupRecyclerView(); updateAdapter(); @@ -835,22 +835,24 @@ public class TimelineFragment extends SFragment implements } private void onFetchTimelineFailure(Exception exception, FetchEnd fetchEnd, int position) { - swipeRefreshLayout.setRefreshing(false); + if(isAdded()) { + swipeRefreshLayout.setRefreshing(false); - if (fetchEnd == FetchEnd.MIDDLE && !statuses.get(position).isRight()) { - Placeholder placeholder = statuses.get(position).getAsLeftOrNull(); - StatusViewData newViewData; - if (placeholder == null) { - placeholder = newPlaceholder(); + if (fetchEnd == FetchEnd.MIDDLE && !statuses.get(position).isRight()) { + Placeholder placeholder = statuses.get(position).getAsLeftOrNull(); + StatusViewData newViewData; + if (placeholder == null) { + placeholder = newPlaceholder(); + } + newViewData = new StatusViewData.Placeholder(placeholder.id, false); + statuses.setPairedItem(position, newViewData); + updateAdapter(); } - newViewData = new StatusViewData.Placeholder(placeholder.id, false); - statuses.setPairedItem(position, newViewData); - updateAdapter(); - } - Log.e(TAG, "Fetch Failure: " + exception.getMessage()); - fulfillAnyQueuedFetches(fetchEnd); - progressBar.setVisibility(View.GONE); + Log.e(TAG, "Fetch Failure: " + exception.getMessage()); + fulfillAnyQueuedFetches(fetchEnd); + progressBar.setVisibility(View.GONE); + } } private void fulfillAnyQueuedFetches(FetchEnd fetchEnd) { @@ -1055,10 +1057,12 @@ public class TimelineFragment extends SFragment implements private final ListUpdateCallback listUpdateCallback = new ListUpdateCallback() { @Override public void onInserted(int position, int count) { - adapter.notifyItemRangeInserted(position, count); - Context context = getContext(); - if (position == 0 && context != null) { - recyclerView.scrollBy(0, Utils.dpToPx(context, -30)); + if(isAdded()) { + adapter.notifyItemRangeInserted(position, count); + Context context = getContext(); + if (position == 0 && context != null) { + recyclerView.scrollBy(0, Utils.dpToPx(context, -30)); + } } }