java.lang.IllegalStateException: Fragment not attached to Activity

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

    In Fragment use isAdded() It will return true if the fragment is currently attached to Activity.

    If you want to check inside the Activity

     Fragment fragment = new MyFragment();
       if(fragment.getActivity()!=null)
          { // your code here}
          else{
           //do something
           }
    

    Hope it will help someone

提交回复
热议问题