My app uses android navigation component. There is case where IllegalStateException: Fragment not attached to an activity is logged. I couldn't understand this. The flow is as follows:
The activity_layout has this code:
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:defaultNavHost="true"
app:navGraph="@navigation/buy_graph" />
The nav graph contains all fragments in the journey. When a webservice is called, it updates the values in the viewmodel and user proceeds to next step in the journey. From one of the fragments, when this happened the user was supposed to goto other activity, but IllegalStateException was thrown with detail fragmentXXX not attached to the activity. The intent that was to be called was this one:
val intent = Intent(requireActivity(), MatchesActivity::class.java)
intent.putExtra("id", viewModel._id)
startActivity(intent)
requireActivity().finish()
This is not happening everytime. It is logged in crashlytics. The logs are as follows:
Fatal Exception: java.lang.IllegalStateException: Fragment Step8Fragment{e918a72} (0fededbf-6edd-442a-921c-5a33717b712e) not attached to an activity.
at androidx.fragment.app.Fragment.requireActivity(Fragment.java:928)
at com.view.fragment.Step8Fragment.onStepCompleted(Step8Fragment.kt:114)
at com.view_model.ReqViewModel$submitStep5$stepSuccess$1.invoke(ReqViewModel.kt:831)
at com.view_model.ReqViewModel$submitStep5$stepSuccess$1.invoke(ReqViewModel.kt:37)
at com.view_model.Repo$submitStepData$responseListener$1.onResponse(Repo.kt:104)
at com.view_model.Repo$submitStepData$responseListener$1.onResponse(Repo.kt:16)
at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:90)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7698)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:952)
Not getting any hint why is it happening.