I am rarely getting this error while making an API call.
java.lang.IllegalStateException: Fragment not attached to Activity
I tried puttin
In Fragment use isAdded() It will return true if the fragment is currently attached to Activity.
isAdded()
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