Reputation: 722
I am facing this particular crash on few devices
Fatal Exception: java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1515)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:617)
at net.trellisys.papertrell.components.mediagallery.MGBaseActivity$initQuickControls.doInBackground(MGBaseActivity.java:136)
at net.trellisys.papertrell.components.mediagallery.MGBaseActivity$initQuickControls.doInBackground(MGBaseActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
What I am doing is am loading a Fragment
to an Activity
using an Async
method.
I can not recreate this error in my side but have seen this issue in many devices. Can any one help me out with this error?
Here's the code snippet :-
public class initQuickControls extends AsyncTask<String,Void,String> {
String fromPage = "";
public initQuickControls(String from){
fromPage = from;
}
QuickControlsFragment quicFrag = null;
@Override
protected String doInBackground(String... strings) {
quicFrag = new QuickControlsFragment(fromPage);
FragmentManager fragManager = getSupportFragmentManager();
fragManager.beginTransaction().replace(R.id.quickcontrols_container,quicFrag).commitAllowingStateLoss();
return "EXECUTED";
}
protected void onPostExecute(String result) {}
}
And this is how I am calling the Fragment
:-
new initQuickControls("List").execute("");
Upvotes: 0
Views: 408
Reputation: 1781
First of all you should be familiar with the activity lifecycle.
I think that your AsynkTask finishes when activity is destroyed and that's why you cannot perform any fragment transaction. You may add condition before this transaction:
if (!MainActivity.this.isFinishing()) {...}
Hope it'll help.
But if you're using AsynkTask
only to add fragment - this is incorrect. Just add your fragment in the onCreate
method without any additional code.
edit:
You cannot perform UI operations in the doInBackground
method because it's running not in the UI thread. Move Ui-specific code to onPostExecute
method and it'll work. And do not forget about isFinishing()
condition. Also you should cancel all your AsyncTasks
in Activity.onBackPressed()
method to avoid such problems.
Upvotes: 2