Skip to content

Commit dd15893

Browse files
committed
remove existing comment replies screens before adding new one. Also close all comment replies screens when the video details screen is closed.
1 parent 239f6c9 commit dd15893

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,6 +2446,7 @@ private void setOverlayLook(final AppBarLayout appBar,
24462446
// SlideOffset < 0 when mini player is about to close via swipe.
24472447
// Stop animation in this case
24482448
if (behavior == null || slideOffset < 0) {
2449+
NavigationHelper.closeCommentRepliesFragments(activity);
24492450
return;
24502451
}
24512452
binding.overlayLayout.setAlpha(Math.min(MAX_OVERLAY_ALPHA, 1 - slideOffset));

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+
// This removes all of the top CommentRepliesFragment instances.
513+
// Ideally there should only be one since we remove existing before opening a new one.
514+
public static void closeCommentRepliesFragments(@NonNull final FragmentActivity activity) {
515+
final FragmentManager fm = activity.getSupportFragmentManager();
516+
517+
// Remove all existing fragment instances tagged as CommentRepliesFragment
518+
final FragmentTransaction tx = defaultTransaction(fm);
519+
boolean removed = false;
520+
for (final Fragment fragment : fm.getFragments()) {
521+
if (fragment != null && CommentRepliesFragment.TAG.equals(fragment.getTag())) {
522+
tx.remove(fragment);
523+
removed = true;
524+
}
525+
}
526+
if (removed) {
527+
tx.commit();
528+
}
529+
530+
// Only pop back stack entries named CommentRepliesFragment.TAG if they are at the top.
531+
while (true) {
532+
final int count = fm.getBackStackEntryCount();
533+
if (count == 0) {
534+
break;
535+
}
536+
final FragmentManager.BackStackEntry topEntry = fm.getBackStackEntryAt(count - 1);
537+
final String topName = topEntry.getName();
538+
if (CommentRepliesFragment.TAG.equals(topName)) {
539+
// Safe to pop the topmost matching entry (it won't remove other unrelated entries)
540+
fm.popBackStackImmediate(topName, FragmentManager.POP_BACK_STACK_INCLUSIVE);
541+
} else {
542+
break;
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)