java.lang.IllegalStateException: Fragment not attached to Activity

前端 未结 12 1199
名媛妹妹
名媛妹妹 2020-11-28 02:28

I am rarely getting this error while making an API call.

java.lang.IllegalStateException: Fragment  not attached to Activity

I tried puttin

12条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 02:48

    So the base idea is that you are running a UI operation on a fragment that is getting in the onDetach lifecycle.

    When this is happening the fragment is getting off the stack and losing the context of the Activity.

    So when you call UI related functions for example calling the progress spinner and you want to leave the fragment check if the Fragment is added to the stack, like this:

    if(isAdded){ progressBar.visibility=View.VISIBLE }

提交回复
热议问题