Jetpack Navigation : Fragment not attached to a context

Viewed 1509

I'm using Jetpack navigation from the latest android architecture components in my app. In some rare cases, I get the following crash:

Fatal Exception: java.lang.IllegalStateException: Fragment NewOfferFragment{40b548a} not attached to a context.
   at android.support.v4.app.Fragment.requireContext(Fragment.java:609)
   at android.support.v4.app.Fragment.getResources(Fragment.java:7673)
   at com.abc.xyz.offer.owner.fragment.NewOfferFragment$onActivityCreated$2.onChanged(NewOfferFragment.kt:1089)
   at android.arch.lifecycle.LiveData.considerNotify(LiveData.java:109)
   at android.arch.lifecycle.LiveData.dispatchingValue(LiveData.java:126)
   at android.arch.lifecycle.LiveData.setValue(LiveData.java:282)
   at android.arch.lifecycle.MutableLiveData.setValue(MutableLiveData.java:33)
   at android.arch.lifecycle.Transformations$2$1.onChanged(Transformations.java:145)
   at android.arch.lifecycle.MediatorLiveData$Source.onChanged(MediatorLiveData.java:152)
   at android.arch.lifecycle.LiveData.considerNotify(LiveData.java:109)
   at android.arch.lifecycle.LiveData.dispatchingValue(LiveData.java:126)
   at android.arch.lifecycle.LiveData.setValue(LiveData.java:282)
   at android.arch.lifecycle.MutableLiveData.setValue(MutableLiveData.java:33)
   at com.abc.xyz.util.remoteUtils.NetworkBoundResource$fetchFromNetwork$2$2.com.abc.xyz.util.remoteUtils.Resource$Companion.error(NetworkBoundResource.kt:3019)
   at android.arch.lifecycle.MediatorLiveData$Source.onChanged(MediatorLiveData.java:152)
   at android.arch.lifecycle.LiveData.considerNotify(LiveData.java:109)
   at android.arch.lifecycle.LiveData.dispatchingValue(LiveData.java:121)
   at android.arch.lifecycle.LiveData.access$000(LiveData.java:59)
   at android.arch.lifecycle.LiveData$ObserverWrapper.shouldBeActive(LiveData.java:416)
   at android.arch.lifecycle.LiveData.observeForever(LiveData.java:207)
   at android.arch.lifecycle.MediatorLiveData$Source.plug(MediatorLiveData.java:141)
   at android.arch.lifecycle.MediatorLiveData.addSource(MediatorLiveData.java:96)
   at com.abc.xyz.util.remoteUtils.NetworkBoundResource$fetchFromNetwork$2.onChanged(NetworkBoundResource.kt:1074)
   at android.arch.lifecycle.MediatorLiveData$Source.onChanged(MediatorLiveData.java:152)
   at android.arch.lifecycle.LiveData.considerNotify(LiveData.java:109)
   at android.arch.lifecycle.LiveData.dispatchingValue(LiveData.java:126)
   at android.arch.lifecycle.LiveData.setValue(LiveData.java:282)
   at android.arch.lifecycle.LiveData$1.run(LiveData.java:87)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:7325)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

The following is the code for my fragment's onActivityCreated:

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    viewModel = ViewModelProviders.of(this, viewModelFactory)[NewOfferViewModel::class.java]
    viewModel.init(offerId)

    binding?.let {
        it.viewModel = viewModel
        it.handler = RadioButtonHandler(this)
    }

    viewModel.result.observe(this, Observer {
        it?.let {
            binding.resource = it
            endProgress()

            Logger.o(Thread.currentThread(), it)
            when (it.status) {
                Status.SUCCESS -> {
                    Logger.i(Thread.currentThread(), it.message)
                    endProgress()
                    if (viewModel.shouldUpload)
                        navController().popBackStack()
                    viewModel.shouldUpload = false
                }

                Status.ERROR -> {
                    Logger.i(Thread.currentThread(), it.message)
                    endProgress()
                    navController().popBackStack()
                    viewModel.shouldUpload = false
                }

                Status.LOADING -> {
                    startProgress()
                }
            }
        }
    })
}

Now I've seen a lot of solutions to the possible causes of this crash but those are for the traditional approach of inflating the fragment via a fragment manager. What might be the possible causes for this crash when using jetpack navigation?

Thank you.

0 Answers
Related