Search code examples
androidkotlinandroid-fragmentsandroid-viewpager2android-viewbinding

Binding becomes when fragment is visible in ViewPager


Problem: I have a ContainerFragment in which there is a ViewPager. CameraFragment is being shown in this viewpager. The shutter button is in ContainerFragment and I'm using an interface to receive callback in CameraFragment when shutter button is clicked in order to click picture. When callback is received then I try to capture image and app crashes with error.

Log:

    java.lang.IllegalStateException: should never call auto-cleared-value get when it might not be available
            at com.example.chat.utils.services.AutoClearedValue.getValue(AutoClearedValue.kt:51)
            at com.example.chat.ui.base.BaseFragment.getBinding(BaseFragment.kt:41)
            at com.example.chat.ui.camera.camera.CameraFragment.captureImage(CameraFragment.kt:127)
    at com.example.chat.ui.camera.camera.CameraFragment.onShutterClicked(CameraFragment.kt:135) 
at com.example.chat.ui.camera.camerafragmentcontainer.CameraContainerFragment.handleShutterClick(CameraContainerFragment.kt:94)

Solution

  • Solved this issue. The problem was that when I was initializing the interface I was creating an instance of CameraFragment() and again in viewpager a new instance was being created. so just passed the same instance in viewpager and the problem solved.