We are using the Google Play In-App Reviews API.
Since our last update to the Play Store (last week), we started receiving the following exception java.lang.IllegalStateException, inside of the following listeners: request.addOnCompleteListener and request.addOnFailureListener

In addOnCompleteListener it fails when we call findNavController() and in addOnFailureListener when we call requireActivity().
All this happens inside a Fragment and the listeners are called from a function (requestForReviewIfReady) inside onResume(), so findNavController() and requireActivity() should not throw any exceptions as far as I’m aware.

So far, we have had 3 crash reports and it seems to be happening only in a couple of devices (Redmi 9A (twice) and LGE LG G6) and APIs (9 & 10 (twice)).

The (simplified) code is the following:


private fun requestForReviewIfReady() {
        val navController = findNavController() //(1) First call to findNavController(). Here it doesn't fail.
        val manager = context?.let { ReviewManagerFactory.create(it) }
        var reviewInfo: ReviewInfo? = null

        if (someConditions) {

            val request = manager?.requestReviewFlow()

            request?.addOnCompleteListener { requestTask ->
                if (requestTask.isSuccessful) {

                    reviewInfo = requestTask.result

                    with(navController) {
                        if (currentBackStackEntry?.destination == graph[R.id.mainFragment]) {
                          //Some unimportant code
                          val navController_2 = findNavController() //(2) Second call to findNavController. It throws an IllegalStateException.

                        }
                    }

                } else {
                    // There was some problem, continue regardless of the result.
                }
            }

            request?.addOnFailureListener {
                //Some unimportant code
                isTestLabDevice(requireActivity()) //(3) Throws IllegalStateException
            }

        }
    }

As you can see, at the beginning of requestForReviewIfReady() we call findNavController (1) and it never fails. While in (2) it throws the exception. We call findNavController again because in the full code we call it just at the beginning of another function (navigateOnlyIfCurrentDestinationIsMainFragment()).

The stack traces are the following:


java.lang.IllegalStateException:    at androidx.fragment.app.Fragment.getParentFragmentManager(Fragment.java:1059)
    at androidx.navigation.fragment.NavHostFragment$Companion.findNavController(NavHostFragment.java:375)
    at androidx.navigation.fragment.FragmentKt.findNavController(Fragment.kt:29)
    at com.example.myApp.mainscreen.MainFragment.navigateOnlyIfCurrentDestinationIsMainFragment(MainFragment.kt:864)
    at com.example.myApp.mainscreen.MainFragment.requestForReviewIfReady$lambda-68(MainFragment.kt:1190)
    at com.example.myApp.mainscreen.MainFragment$$InternalSyntheticLambda$0$bccfb11977a9fae7720b93d357945e2b1fc482677438610617a9f85833e8f048$1.onComplete(MainFragment.java:0)
    at com.google.android.play.core.tasks.zza.run(com.google.android.play:core@@1.10.3:1)
  at android.os.Handler.handleCallback (Handler.java:914)
  at android.os.Handler.dispatchMessage (Handler.java:100)
  at android.os.Looper.loop (Looper.java:225)
  at android.app.ActivityThread.main (ActivityThread.java:7563)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:539)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:994)

Following the first stack trace we see that the exception is thrown when fragmentManager == null.

java.lang.IllegalStateException:    at androidx.fragment.app.Fragment.requireActivity(Fragment.java:947)
    at com.example.myApp.mainscreen.MainFragment.changeAdsBackgroundColor(MainFragment.kt:778)
    at com.example.myApp.mainscreen.MainFragment.loadBannerAdWhenAppropriate(MainFragment.kt:760)
    at com.example.myApp.mainscreen.MainFragment.requestForReviewIfReady$lambda-69(MainFragment.kt:1202)
    at com.example.myApp.mainscreen.MainFragment$$InternalSyntheticLambda$0$bccfb11977a9fae7720b93d357945e2b1fc482677438610617a9f85833e8f048$2.onFailure(MainFragment.java:0)
    at com.google.android.play.core.tasks.zzc.run(zzc.java:1)
    at com.google.android.gms.ads.nonagon.signalgeneration.zzo.run$bridge(com.google.android.gms:play-services-ads@@20.6.0:0)
  at android.os.Handler.handleCallback (Handler.java:914)
  at android.os.Handler.dispatchMessage (Handler.java:100)
  at android.os.Looper.loop (Looper.java:225)
  at android.app.ActivityThread.main (ActivityThread.java:7563)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:539)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:994)


Following the second stack trace we see that the exception is thrown when activity == null

0

There are 0 best solutions below