Catch IllegalStateException during ViewPager state restoration of missing fragments - #13799
Open
animesh68 wants to merge 2 commits into
Open
Catch IllegalStateException during ViewPager state restoration of missing fragments#13799animesh68 wants to merge 2 commits into
animesh68 wants to merge 2 commits into
Conversation
|
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! |
Contributor
|
Please follow the contribution guidelines and use the PR template. |
Author
|
Hey @TobiGr , thanks for pointing that out , I'll update the description to follow the template properly. |
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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



What is it?
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 inMainFragment$SelectedTabsPagerAdapter.restoreStateduring state restoration of the home tabsViewPager.In modern versions of the AndroidX Fragment library,
FragmentManager.getFragment(bundle, key)throws anIllegalStateExceptionrather than returningnullif the key exists in the bundle but the fragment's unique"who"ID is not registered in the currentFragmentManager's active list. This state mismatch occurs when the childFragmentManageris torn down and recreated (such as during back-stack pops) between when theViewPagersaves its state and when it is restored. BecauseFragmentStatePagerAdapteris 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 atry/catchblock forIllegalStateException. If caught, the adapter logs a warning (W/FragmentStatePagerAdapt: Bad fragment at key ...) and usescontinueto 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
FragmentStatePagerAdapterMenuWorkaroundTestthat mocks theFragmentManagerandBundlestate. 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
Fixes the following issue(s)
Relies on the following changes
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