0

In my recent app version, I am seeing a lot of logs related to this issue:

java.lang.IllegalStateException: Method addObserver must be called on the main thread androidx.lifecycle.LifecycleRegistry.enforceMainThreadIfNeeded(LifecycleRegistry.java:317) androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:172) androidx.fragment.app.Fragment.initLifecycle(Fragment.java:550) androidx.fragment.app.Fragment.<init>(Fragment.java:516) androidx.fragment.app.DialogFragment.<init>(DialogFragment.java:181) com.example.testing.biometric.BiometricDialogFragment.<init>(BiometricDialogFragment.kt:13) com.example.testing.biometric.BiometricDialogFragment$Companion.show(BiometricDialogFragment.kt:17)

I am not able to understand if this is really crashing the app because I am unable to replicate it on my devices. Inside my fragment, I have this method:

companion object {
        fun show(fragmentManager: FragmentManager) {
             BiometricDialogFragment().show(fragmentManager, TAG)
        }
}

We are calling this method from another fragment based on certain conditions like this:

private fun biometricLogin() {
    helper.getBiometricInfo(this, object : BiometricValidationCallback {
        override fun onSuccess(account: Account) {
            //handle success
        }

        override fun onFail(fail: BiometricFailure) {
            when (fail) {
                BiometricFailure.FailOther -> Biometric-Fragment.show(childFragmentManager)
                else -> {}
            }
        }
    })
}

I have checked multiple posts about this error but none of the solutions seems to work for me.

I am using the following versions of the androidx libraries:

implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0"
implementation "android.arch.lifecycle:common-java8:1.1.1"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"
implementation "androidx.lifecycle:lifecycle-reactivestreams:2.2.0"
implementation "androidx.activity:activity:1.5.0"
implementation "androidx.fragment:fragment-ktx:1.3.0-alpha06"
implementation "androidx.activity:activity-ktx:1.5.0"

implementation "androidx.biometric:biometric:1.0.1"

I am really unable to find the root cause. We are not setting any observers specifically but I dont know why this error is being shown. Also on my devices running on Android 13, I have not been able to replicate it. Whereas this error is being shown for other users on Android 13.

KsiAndroid
  • 21
  • 4

1 Answers1

0

Finally found the rootcause for this. The culprit was the androidx activity and fragment dependencies.

Upon research, I found out that the older versions of the androidx activity and fragments allowed display of fragments normally. But after updating them to 1.5.0 and above, the rules have been changed. Calling from a main thread would result in the above error. So add runOnUiThread and call the show method of the dialogFragment

KsiAndroid
  • 21
  • 4