1

I'm trying to call fragment from method onPostExecute (asynctask)...

this is my code...

@Override
    protected void onPostExecute(String result) {
        if (processVisibility != false) {
            pd.dismiss();
            Toast.makeText(mContext, sendingMessageSuccess,
                        Toast.LENGTH_SHORT).show();

            FragmentExample4 fragment = new FragmentExample4();
            android.support.v4.app.FragmentTransaction fragmentTransaction =
                    getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();
        }
        super.onPostExecute(result);
    }

and when enter in method onPostExecute my application crash and i receive this error...

03-01 17:13:42.141  29230-29230/copyworld.assistenzamobile E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Activity has been destroyed
        at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1511)
        at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
        at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)
        at copyworld.assistenzamobile.backgroundMail.BackgroundMail$startSendingEmail.onPostExecute(BackgroundMail.java:145)
        at copyworld.assistenzamobile.backgroundMail.BackgroundMail$startSendingEmail.onPostExecute(BackgroundMail.java:95)
        at android.os.AsyncTask.finish(AsyncTask.java:631)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:175)
        at android.app.ActivityThread.main(ActivityThread.java:5279)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)

I also try this...

 @Override
    protected void onPostExecute(String result) {
        if (processVisibility != false) {
            pd.dismiss();
            Toast.makeText(mContext, sendingMessageSuccess,
                        Toast.LENGTH_SHORT).show();

            FragmentExample4 fragment = new FragmentExample4();
            android.support.v4.app.FragmentTransaction fragmentTransaction =
                    getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);

            if (!isFinishing()) {
                fragmentTransaction.commit();
            }
        }
        super.onPostExecute(result);
    }

but this also not work and i receive the same problem..

someone can help me???

Sorry for my english

Edoardo Goffredo
  • 303
  • 2
  • 5
  • 20
  • Maybe you find this useful: http://stackoverflow.com/questions/15207305/getting-the-error-java-lang-illegalstateexception-activity-has-been-destroyed – Ollie Strevel Mar 01 '16 at 17:08

1 Answers1

1

AsyncTasks continue to run after the activity has been destroyed. What happens here is that your async task completes after the activity is destroyed and tries to access the now dead activity, throwing the exception - you can't add a fragment to an activity that has finished. You have to check in onPostExecute if the activity is still alive.

AsyncTasks are tricky because they hold a reference to the activity and can cause this kind of issue (where the activity is dead when the task completes). You could consider a fragment with setRetainInstance(true) to run your async tasks, or maybe an IntentService.

Francesc
  • 25,014
  • 10
  • 66
  • 84
  • ... and how i do for start this fragment after asynctask finished?? – Edoardo Goffredo Mar 01 '16 at 17:35
  • For example i have a activity that have a button.. when i click this button, start async task that is in other activity and after asyncytask finished, i want start fragment – Edoardo Goffredo Mar 01 '16 at 17:40
  • You should start the async task in the activity that is going to display the fragment. So if activity A starts activity B and you want to display the fragment in activity B after the async task completes, then run the async task in activity B, not A. You have to be wary of the life cycle though, activity B could be paused or destroyed (the user can press the home button to suspend the activity, or rotate the device which will destroy the activity). You have to handle all these scenarios. – Francesc Mar 01 '16 at 17:48