Skip to content

Commit 9f8055f

Browse files
committed
remove existing comment replies screens before adding new one
1 parent 239f6c9 commit 9f8055f

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,11 @@ public void handleSuggestions(@NonNull final List<SuggestionItem> suggestions) {
10091009
Log.d(TAG, "handleSuggestions() called with: suggestions = [" + suggestions + "]");
10101010
}
10111011
suggestionListAdapter.submitList(suggestions,
1012-
() -> searchBinding.suggestionsList.scrollToPosition(0));
1012+
() -> {
1013+
if (searchBinding != null) {
1014+
searchBinding.suggestionsList.scrollToPosition(0);
1015+
}
1016+
});
10131017

10141018
if (suggestionsPanelVisible && isErrorPanelVisible()) {
10151019
hideLoading();

app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,13 +501,49 @@ public static void openCommentAuthorIfPresent(@NonNull final FragmentActivity ac
501501

502502
public static void openCommentRepliesFragment(@NonNull final FragmentActivity activity,
503503
@NonNull final CommentsInfoItem comment) {
504+
closeCommentRepliesFragments(activity);
504505
defaultTransaction(activity.getSupportFragmentManager())
505506
.replace(R.id.fragment_holder, new CommentRepliesFragment(comment),
506507
CommentRepliesFragment.TAG)
507508
.addToBackStack(CommentRepliesFragment.TAG)
508509
.commit();
509510
}
510511

512+
/**
513+
* Closes all open {@link CommentRepliesFragment}s in {@code activity},
514+
* including those that are not at the top of the back stack.
515+
* This is needed to prevent multiple open CommentRepliesFragments
516+
* Ideally there should only be one since we remove existing before opening a new one.
517+
* @param activity the activity in which to close the CommentRepliesFragments
518+
*/
519+
public static void closeCommentRepliesFragments(@NonNull final FragmentActivity activity) {
520+
final FragmentManager fm = activity.getSupportFragmentManager();
521+
522+
// Remove all existing fragment instances tagged as CommentRepliesFragment
523+
final FragmentTransaction tx = defaultTransaction(fm);
524+
boolean removed = false;
525+
for (final Fragment fragment : fm.getFragments()) {
526+
if (fragment != null && CommentRepliesFragment.TAG.equals(fragment.getTag())) {
527+
tx.remove(fragment);
528+
removed = true;
529+
}
530+
}
531+
if (removed) {
532+
tx.commit();
533+
}
534+
535+
// Only pop back stack entries named CommentRepliesFragment.TAG if they are at the top.
536+
while (fm.getBackStackEntryCount() > 0
537+
&& CommentRepliesFragment.TAG.equals(
538+
fm.getBackStackEntryAt(fm.getBackStackEntryCount() - 1).getName()
539+
)
540+
) {
541+
fm.popBackStackImmediate(CommentRepliesFragment.TAG,
542+
FragmentManager.POP_BACK_STACK_INCLUSIVE);
543+
}
544+
545+
}
546+
511547
public static void openPlaylistFragment(final FragmentManager fragmentManager,
512548
final int serviceId, final String url,
513549
@NonNull final String name) {

0 commit comments

Comments
 (0)