[Solved] java.lang.IllegalStateException: Fragment not attached to Activity

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

java.lang.IllegalStateException: Fragment  not attached to Activity

I tried putting the code inside isAdded() method to check whether fragment is currently added to its activity but still i rarely gets this error. I fail to understand why I am still getting this error. How can i prevent it?

Its showing error on the line-


Below is the sample api call that i am making.

                new APIResponseListener() {
                    public void onResponse(Object response) {

                        cameraInfo = new SInfo();
                        if(isAdded()) {


                    public void onError(VolleyError error) {
                        if (error instanceof NoConnectionError) {
                            String errormsg = getResources().getString(R.string.no_internet_error_msg);
                            Toast.makeText(getActivity(), errormsg, Toast.LENGTH_LONG).show();

Solution #1:

This error happens due to the combined effect of two factors:

  • The HTTP request, when complete, invokes either onResponse() or onError() (which work on the main thread) without knowing whether the Activity is still in the foreground or not. If the Activity is gone (the user navigated elsewhere), getActivity() returns null.
  • The Volley Response is expressed as an anonymous inner class, which implicitly holds a strong reference to the outer Activity class. This results in a classic memory leak.

To solve this problem, you should always do:

Activity activity = getActivity();
if(activity != null){

    // etc ...


and also, use isAdded() in the onError() method as well:

public void onError(VolleyError error) {

    Activity activity = getActivity(); 
    if(activity != null && isAdded())
        if (error instanceof NoConnectionError) {
           String errormsg = getResources().getString(R.string.no_internet_error_msg);
           Toast.makeText(activity, errormsg, Toast.LENGTH_LONG).show();
Respondent: Y.S

Solution #2:

Fragment lifecycle is very complex and full of bugs, try to add:

Activity activity = getActivity(); 
if (isAdded() && activity != null) {
Respondent: Miroslav Michalec

Solution #3:

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:


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

Respondent: Dharmesh Baldha

Solution #4:

Exception: java.lang.IllegalStateException: Fragment

DeadlineListFragment{ad2ef970} not attached to Activity

Category: Lifecycle

Description: When doing time-consuming operation in background thread(e.g, AsyncTask), a new Fragment has been created in the meantime, and was detached to the Activity before the background thread finished. The code in UI thread(e.g.,onPostExecute) calls upon a detached Fragment, throwing such exception.

Fix solution:

  1. Cancel the background thread when pausing or stopping the

  2. Use isAdded() to check whether the fragment is attached
    and then to getResources() from activity.

Respondent: Rahil Ali

Solution #5:

i may be late but may help someone …..
The best solution for this is to create a global application class instance and call it in the particular fragment where your activity is not being attached

as like below

icon = MyApplication.getInstance().getString(R.string.weather_thunder);

Here is application class

public class MyApplication extends Application {

    private static MyApplication mInstance;
    private RequestQueue mRequestQueue;

    public void onCreate() {
        mInstance = this;

    public static synchronized MyApplication getInstance() {
        return mInstance;
Respondent: md gouse

Solution #6:

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();
      { // your code here}
       //do something

Hope it will help someone

Respondent: Prateek218

Solution #7:

This error can happen if you are instantiating a fragment that somehow can’t be instantiated:

Fragment myFragment = MyFragment.NewInstance();

public classs MyFragment extends Fragment {
  public void onCreate() {
   // Some error here, or anywhere inside the class is preventing it from being instantiated

In my case, i have met this when i tried to use:

private String loading = getString(R.string.loading);
Respondent: sagits

Solution #8:

I adopted the following approach for handling this issue. Created a new class which act as a wrapper for activity methods like this

public class ContextWrapper {
    public static String getString(Activity activity, int resourceId, String defaultValue) {
        if (activity != null) {
            return activity.getString(resourceId);
        } else {
            return defaultValue;

    //similar methods like getDrawable(), getResources() etc


Now wherever I need to access resources from fragments or activities, instead of directly calling the method, I use this class. In case the activity context is not null it returns the value of the asset and in case the context is null, it passes a default value (which is also specified by the caller of the function).

Important This is not a solution, this is an effective way where you can handle this crash gracefully. You would want to add some logs in cases where you are getting activity instance as null and try to fix that, if possible.

Respondent: Ezio

Solution #9:

this happen when the fragment does not have a context ,thus the getActivity()method return null.
check if you use the context before you get it?or if the Activity is not exist anymore . use context in fragment.onCreate and after api response usually case this problem

Respondent: dexian fan

Solution #10:

Sometimes this exception is caused by a bug in the support library implementation. Recently I had to downgrade from 26.1.0 to 25.4.0 to get rid of it.

Respondent: Bord81

Solution #11:

This issue occurs whenever you call a context which is unavailable or null when you call it. This can be a situation when you are calling main activity thread’s context on a background thread or background thread’s context on main activity thread.

For instance , I updated my shared preference string like following.


And called finish() right after it. Now what it does is that as commit runs on main thread and stops any other Async commits if coming until it finishes. So its context is alive until the write is completed. Hence previous context is live , causing the error to occur.

So make sure to have your code rechecked if there is some code having this context issue.

Respondent: Prashant Paliwal

Solution #12:

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 }

Respondent: Martin Gelevski

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy