From 9f8055f018f62c58c07e687980dcbfd3aae05d2c Mon Sep 17 00:00:00 2001 From: vt Date: Sun, 8 Feb 2026 13:07:23 +0530 Subject: [PATCH] remove existing comment replies screens before adding new one --- .../fragments/list/search/SearchFragment.java | 6 +++- .../schabi/newpipe/util/NavigationHelper.java | 36 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java index 8cb5f6497..5c0d7d321 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java @@ -1009,7 +1009,11 @@ public class SearchFragment extends BaseListFragment searchBinding.suggestionsList.scrollToPosition(0)); + () -> { + if (searchBinding != null) { + searchBinding.suggestionsList.scrollToPosition(0); + } + }); if (suggestionsPanelVisible && isErrorPanelVisible()) { hideLoading(); diff --git a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java index f702c5bd5..bfdf6a02c 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -501,6 +501,7 @@ public final class NavigationHelper { public static void openCommentRepliesFragment(@NonNull final FragmentActivity activity, @NonNull final CommentsInfoItem comment) { + closeCommentRepliesFragments(activity); defaultTransaction(activity.getSupportFragmentManager()) .replace(R.id.fragment_holder, new CommentRepliesFragment(comment), CommentRepliesFragment.TAG) @@ -508,6 +509,41 @@ public final class NavigationHelper { .commit(); } + /** + * Closes all open {@link CommentRepliesFragment}s in {@code activity}, + * including those that are not at the top of the back stack. + * This is needed to prevent multiple open CommentRepliesFragments + * Ideally there should only be one since we remove existing before opening a new one. + * @param activity the activity in which to close the CommentRepliesFragments + */ + public static void closeCommentRepliesFragments(@NonNull final FragmentActivity activity) { + final FragmentManager fm = activity.getSupportFragmentManager(); + + // Remove all existing fragment instances tagged as CommentRepliesFragment + final FragmentTransaction tx = defaultTransaction(fm); + boolean removed = false; + for (final Fragment fragment : fm.getFragments()) { + if (fragment != null && CommentRepliesFragment.TAG.equals(fragment.getTag())) { + tx.remove(fragment); + removed = true; + } + } + if (removed) { + tx.commit(); + } + + // Only pop back stack entries named CommentRepliesFragment.TAG if they are at the top. + while (fm.getBackStackEntryCount() > 0 + && CommentRepliesFragment.TAG.equals( + fm.getBackStackEntryAt(fm.getBackStackEntryCount() - 1).getName() + ) + ) { + fm.popBackStackImmediate(CommentRepliesFragment.TAG, + FragmentManager.POP_BACK_STACK_INCLUSIVE); + } + + } + public static void openPlaylistFragment(final FragmentManager fragmentManager, final int serviceId, final String url, @NonNull final String name) {