Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Prevent IllegalStateException when orphaned Fragments try to popBackStack() (EXPOSUREAPP-4430) #1987

Merged
merged 3 commits into from
Dec 23, 2020

Conversation

d4rken
Copy link
Member

@d4rken d4rken commented Dec 22, 2020

Prevent IllegalStateException if a fragment that's not added to an Activity tries to popBackStack().

We've got a few stacktraces where the app crashes with this stacktrace (see below).

It looks like the Fragment is not added to an Activity and then we call popBackStack which throws due not not being able to find a fragment manager.

While I can't reproduce it, I think what happens is:

  • User clicks "remove test"
  • Then the activity is recreated, maybe because the app was put into the background, there may be other events that could get us into this state.
  • When the user resumes the app the dialog is not dismissed (although it should dissmiss, and does so for me)
  • The restored dialog has a reference to the previous fragment instance and calls popBackStack on that leading to the crash.

As this may be a more common error to encounter, I've added the fix (guard clause) to the extension function. "If a fragment is not added to an activity, don't popBackStack().

Originates from here:

java.lang.IllegalStateException: 
  at androidx.fragment.app.Fragment.getParentFragmentManager (Fragment.java:2)
  at androidx.navigation.fragment.NavHostFragment.findNavController (NavHostFragment.java:3)
  at com.google.android.gms.common.internal.Preconditions.popBackStack
  at de.rki.coronawarnapp.ui.submission.testresult.pending.SubmissionTestResultPendingFragment.access$navigateToMainScreen (SubmissionTestResultPendingFragment.kt:1)
  at de.rki.coronawarnapp.ui.submission.testresult.pending.SubmissionTestResultPendingFragment$buildErrorDialog$1.invoke (SubmissionTestResultPendingFragment.kt:2)
  at de.rki.coronawarnapp.util.DialogHelper$showDialog$$inlined$let$lambda$1.onClick (DialogHelper.kt:3)
  at androidx.appcompat.app.AlertController$ButtonHandler.handleMessage (AlertController.java:3)
  at android.os.Handler.dispatchMessage (Handler.java:107)
  at android.os.Looper.loop (Looper.java:237)
  at android.app.ActivityThread.main (ActivityThread.java:7948)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1075)
``

@d4rken d4rken added bug Something isn't working maintainers Tag pull requests created by maintainers labels Dec 22, 2020
@d4rken d4rken added this to the 1.11.0 milestone Dec 22, 2020
@d4rken d4rken requested a review from a team December 22, 2020 17:52
@ralfgehrer ralfgehrer self-assigned this Dec 23, 2020
@chiljamgossow chiljamgossow self-assigned this Dec 23, 2020
@sonarcloud
Copy link

sonarcloud bot commented Dec 23, 2020

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

@ralfgehrer ralfgehrer merged commit 5f4524c into release/1.11.x Dec 23, 2020
@ralfgehrer ralfgehrer deleted the fix/4430-backstack-crash branch December 23, 2020 11:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working maintainers Tag pull requests created by maintainers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants