java.lang.IllegalStateException: Fragment not attached to Activity

前端 未结 12 1161
名媛妹妹
名媛妹妹 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:38

    I Found Very Simple Solution isAdded() method which is one of the fragment method to identify that this current fragment is attached to its Activity or not.

    we can use this like everywhere in fragment class like:

    if(isAdded())
    {
    
    // using this method, we can do whatever we want which will prevent   **java.lang.IllegalStateException: Fragment not attached to Activity** exception.
    
    }
    

提交回复
热议问题