Skip to content

Catch IllegalStateException during ViewPager state restoration of missing fragments - #13799

Open
animesh68 wants to merge 2 commits into
TeamNewPipe:devfrom
animesh68:fix/13740-viewpager-restore-crash
Open

Catch IllegalStateException during ViewPager state restoration of missing fragments#13799
animesh68 wants to merge 2 commits into
TeamNewPipe:devfrom
animesh68:fix/13740-viewpager-restore-crash

Conversation

@animesh68

@animesh68 animesh68 commented Aug 28, 2026

Copy link
Copy Markdown

What is it?

  • Bugfix (user facing)
  • Feature (user facing)
  • Codebase improvement (dev facing)
  • Meta improvement to the project (dev facing)

Description of the changes in your PR

Problem & Root Cause

An intermittent crash (java.lang.IllegalStateException: Fragment no longer exists for key...) occurs when performing gesture back-navigation immediately after searching. This surfaces in MainFragment$SelectedTabsPagerAdapter.restoreState during state restoration of the home tabs ViewPager.

In modern versions of the AndroidX Fragment library, FragmentManager.getFragment(bundle, key) throws an IllegalStateException rather than returning null if the key exists in the bundle but the fragment's unique "who" ID is not registered in the current FragmentManager's active list. This state mismatch occurs when the child FragmentManager is torn down and recreated (such as during back-stack pops) between when the ViewPager saves its state and when it is restored. Because FragmentStatePagerAdapter is a deprecated class vendored directly into the NewPipe codebase, we cannot resolve this via upstream dependency updates and must fix it locally.

The Fix

We wrap the call to mFragmentManager.getFragment(bundle, key) in a try/catch block for IllegalStateException. If caught, the adapter logs a warning (W/FragmentStatePagerAdapt: Bad fragment at key ...) and uses continue to safely skip restoring that tab index.

Verification
  • Code Quality: Verified that the project compiles cleanly (./gradlew assembleDebug) and passes all style audits (./gradlew :app:runCheckstyle).

  • Unit Tests: Created a new unit test suite FragmentStatePagerAdapterMenuWorkaroundTest that mocks the FragmentManager and Bundle state. Confirmed the test passes (./gradlew :app:testDebugUnitTest), verifying the exception is caught and logged.

  • Repro Scenarios: We attempted to reproduce the crash on an Android 15 (API 35) emulator under multiple scenarios (standard back-navigation, screen rotation, and simulated background process death). The app successfully restored state without throwing in all cases. This suggests the crash is timing-sensitive (e.g. races with Android's predictive back swipe-previews) or OS-specific (e.g. GrapheneOS's aggressive background memory limits).

AI disclosure

The Antigravity AI coding assistant generated the initial fix and the unit test, and executed the build, checkstyle, and emulator verification runs under my close direction. I critically reviewed and validated all code, logs, and outputs to ensure technical accuracy.

Before/After Screenshots/Screen Record

  • Before: N/A (Non-visual bugfix to prevent a crash)
  • After: N/A (Non-visual bugfix to prevent a crash)

Fixes the following issue(s)

Relies on the following changes

  • None

APK testing

The APK can be found by going to the "Checks" tab below the title. On the left pane, click on "CI", scroll down to "artifacts" and click "app" to download the zip file which contains the debug APK of this PR.

Due diligence

@github-actions github-actions Bot added the size/medium PRs with less than 250 changed lines label Aug 28, 2026
@sonarqubecloud

Copy link
Copy Markdown

@ShareASmile ShareASmile added the bug Issue is related to a bug label Aug 29, 2026
@AudricV AudricV added the template missing The bug/feature template is missing. No further action is taken until the template is applied. label Aug 29, 2026
@animesh68

Copy link
Copy Markdown
Author

Hey @ShareASmile , thanks for labeling the issue! Just wanted to flag that the PR (#13799) is still waiting on a review whenever you or another maintainer has a chance — checks are passing. No rush!

@TobiGr

TobiGr commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Please follow the contribution guidelines and use the PR template.

@animesh68

Copy link
Copy Markdown
Author

Hey @TobiGr , thanks for pointing that out , I'll update the description to follow the template properly.

@animesh68

Copy link
Copy Markdown
Author

Hey @TobiGr, just following up! I've updated the PR description to follow the official template as requested. Whenever you or the team have some time, I'd appreciate a review. Let me know if there is anything else I need to adjust. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue is related to a bug size/medium PRs with less than 250 changed lines template missing The bug/feature template is missing. No further action is taken until the template is applied.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash when going back from the search results

4 participants