Kartik_Koro
Kartik_Koro

Reputation: 1287

How can I perform AsyncTask in onDetach() of Fragment (IllegalStateException)

I have a fragment, and when that fragment is completely removed from the fragment(not just when it is added to backStack), I need to tell the server to unlock an entity on serverside.(It is locked when the fragment is created). So my onDetach() method is as follows:

@Override 
    public void onDetach(){

        new UnlockEntityAsyncTask().execute();
        Log.v("ResultViewFragment", "View onDetach called");

        super.onDetach();
    }

Sadly, By the time the network operation starts, the fragment is no longer attached to activity and I get IllegalStateException:

07-11 15:55:08.600: E/AndroidRuntime(4239): java.lang.RuntimeException: An error occured while executing doInBackground()
07-11 15:55:08.600: E/AndroidRuntime(4239):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
07-11 15:55:08.600: E/AndroidRuntime(4239):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-11 15:55:08.600: E/AndroidRuntime(4239):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-11 15:55:08.600: E/AndroidRuntime(4239):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-11 15:55:08.600: E/AndroidRuntime(4239):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-11 15:55:08.600: E/AndroidRuntime(4239):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
07-11 15:55:08.600: E/AndroidRuntime(4239):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-11 15:55:08.600: E/AndroidRuntime(4239):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-11 15:55:08.600: E/AndroidRuntime(4239):     at java.lang.Thread.run(Thread.java:864)
07-11 15:55:08.600: E/AndroidRuntime(4239): Caused by: java.lang.IllegalStateException: Fragment ResultViewFragment{40f26e18} not attached to Activity
07-11 15:55:08.600: E/AndroidRuntime(4239):     at android.support.v4.app.Fragment.getResources(Fragment.java:603)
07-11 15:55:08.600: E/AndroidRuntime(4239):     at android.support.v4.app.Fragment.getString(Fragment.java:625)
07-11 15:55:08.600: E/AndroidRuntime(4239):     at com.amberroad.sigmatickettracker.screen.ResultViewFragment$UnlockEntityAsyncTask.doInBackground(ResultViewFragment.java:395)
07-11 15:55:08.600: E/AndroidRuntime(4239):     at com.amberroad.sigmatickettracker.screen.ResultViewFragment$UnlockEntityAsyncTask.doInBackground(ResultViewFragment.java:1)
07-11 15:55:08.600: E/AndroidRuntime(4239):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
07-11 15:55:08.600: E/AndroidRuntime(4239):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

So I guess the Exception occurs because I am trying to execute code of the fragment when it is not attached to the activity. How do I overcome this?

EDIT

The first line of the doInBackground uses a variable of the Fragment (ResultViewFragment) called mContext which is initialized to getActivity().getApplicationContext() in onCreateView(). This causes a problem?

Upvotes: 0

Views: 403

Answers (1)

vadimvolk
vadimvolk

Reputation: 711

You should send your application context as a parameter to your AsyncTask constructor.

new UnlockEntityAsyncTask(mContext.getApplicationContext());

First line of doInBackground executed at the moment when your mContext is already detached.

Upvotes: 1

Related Questions