From 78d3c142a83f8fe02529d33da47b7b2313446c3c Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Tue, 19 Mar 2024 22:44:13 +0100 Subject: [PATCH 1/3] Make sure the Fragment is attached to the activity onResume --- .../wordpress/android/ui/reader/ReaderPostListFragment.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java index 809ec2bfcaa3..093d91613f35 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java @@ -745,6 +745,9 @@ public void onPause() { @Override public void onResume() { super.onResume(); + if (!isAdded() || getView() == null) { + return; + } /* * This is a workaround for https://github.com/wordpress-mobile/WordPress-Android/issues/11985. * The RecyclerView doesn't get redrawn correctly when the adapter finishes its initialization in onStart. From ae0e62486d9dce4e7e06f8dbacb9897a4dfb3b73 Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Thu, 21 Mar 2024 22:45:03 +0100 Subject: [PATCH 2/3] Check the fragment is still attached to the activity when the updateCurrentTag Thread starts. --- .../android/ui/reader/ReaderPostListFragment.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java index 093d91613f35..38781e2bfc2c 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java @@ -28,6 +28,7 @@ import androidx.appcompat.widget.SearchView; import androidx.core.content.ContextCompat; import androidx.core.text.HtmlCompat; +import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.RecyclerView; @@ -745,9 +746,6 @@ public void onPause() { @Override public void onResume() { super.onResume(); - if (!isAdded() || getView() == null) { - return; - } /* * This is a workaround for https://github.com/wordpress-mobile/WordPress-Android/issues/11985. * The RecyclerView doesn't get redrawn correctly when the adapter finishes its initialization in onStart. @@ -2323,10 +2321,15 @@ private void updateCurrentTagIfTime() { new Thread() { @Override public void run() { + // Check the fragment is attached to the activity when this Thread starts. + FragmentActivity activity = getActivity(); + if (activity == null) { + return; + } if (ReaderTagTable.shouldAutoUpdateTag(getCurrentTag()) && isAdded()) { - requireActivity().runOnUiThread(() -> updateCurrentTag()); + activity.runOnUiThread(() -> updateCurrentTag()); } else { - requireActivity().runOnUiThread(() -> { + activity.runOnUiThread(() -> { if (isBookmarksList() && isPostAdapterEmpty() && isAdded()) { setEmptyTitleAndDescriptionForBookmarksList(); mActionableEmptyView.image.setImageResource( From 88e9cabf460705cf45e24a1e3594443b4729ef13 Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Thu, 21 Mar 2024 23:19:39 +0100 Subject: [PATCH 3/3] Move the code that checks if the Fragment is still attached closer to the code that actually use the activity --- .../android/ui/reader/ReaderPostListFragment.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java index 38781e2bfc2c..fd636a68e3f0 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java @@ -2321,14 +2321,19 @@ private void updateCurrentTagIfTime() { new Thread() { @Override public void run() { - // Check the fragment is attached to the activity when this Thread starts. - FragmentActivity activity = getActivity(); - if (activity == null) { - return; - } if (ReaderTagTable.shouldAutoUpdateTag(getCurrentTag()) && isAdded()) { + // Check the fragment is attached right after `shouldAutoUpdateTag` + FragmentActivity activity = getActivity(); + if (activity == null) { + return; + } activity.runOnUiThread(() -> updateCurrentTag()); } else { + // Check the fragment is attached to the activity when this Thread starts. + FragmentActivity activity = getActivity(); + if (activity == null) { + return; + } activity.runOnUiThread(() -> { if (isBookmarksList() && isPostAdapterEmpty() && isAdded()) { setEmptyTitleAndDescriptionForBookmarksList();