0

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.

Nitish
  • 3,097
  • 13
  • 45
  • 80
  • The error message says that your `submitStepData` asynchronous call returned it's result after the fragment was already destroyed (i.e., the user hit the back button while it was doing your network call). It sounds like you're continuing to hold a reference to the fragment even after it is destroyed - that's a memory leak. Where are you cancelling your submit when your fragment is destroyed? – ianhanniballake May 18 '21 at 13:52
  • @ianhanniballake The fragment implements an interface which is added to the ViewModel. After network call is finished, the viewmodel calls the delegate `interface?.onResult`. Since this listener is not lifecycle aware so the problem is. Am I right? Also, how should I change it? – Nitish May 18 '21 at 14:19

0 Answers0