user2380224
user2380224

Reputation: 21

Does AsynckTask.onPostExecute and FragmentActivity.onResumeFragments run on same thread (ui thread)?

I get ConcurrentModificationException when I traverse the ArrayList. What I did:

1.  activity has a ArrayList<Callable>
2.  add Callable to ArrayList when AsyncTask.onPostExecute
3.  traverse the ArrayList to call the callable in onResumeFragments

How the exception be triggered: I make the AsyncTask.execute() and press home button quickly, when my app come back to foreground, the exception happened.

What confuses me is: if AsyncTask.onPostExecute and FragmentActivity.onResumeFragments run on same thread, there won't be a java.util.ConcurrentModificationException, right?

As I know AsyncTask.onPostExecute runs on ui thread, and FragmentActivity.onResumeFragments should runs on ui thread too, so how java.util.ConcurrentModificationException happend?

This is my exception stack

com.yumei.sdkdemo I/PayMethodActivity: 
  11-10 11:34:35.218 10030-10030/com.yumei.sdkdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.yumei.sdkdemo, PID: 10030
java.lang.RuntimeException: Unable to resume activity {com.yumei.sdkdemo/com.yumei.sdk.PayMethodActivity}: java.util.ConcurrentModificationException
 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2954)
 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2985)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:135)
 at android.app.ActivityThread.main(ActivityThread.java:5235)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:906)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:701)
 Caused by: java.util.ConcurrentModificationException
 at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
 at com.yumei.sdk.PayMethodFragmentActivity.onResumeFragments(PayMethodActivity.java:116)
 at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:451)
 at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:141)
 at android.app.Activity.performResume(Activity.java:6040)
 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2943)
 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2985) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
 at android.os.Looper.loop(Looper.java:135) 
 at android.app.ActivityThread.main(ActivityThread.java:5235) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at java.lang.reflect.Method.invoke(Method.java:372) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:906) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:701)  

thanks for your response.

Upvotes: 2

Views: 165

Answers (2)

Kingfisher Phuoc
Kingfisher Phuoc

Reputation: 8190

Yes, AsyncTask's onPostexcute is run in UI thread, and I dont think it's thread problem. ConcurrentModificationException maybe occurs when you try to modify your Arraylist while looping its elements (such as remove Callable in side for loop).
p/s: you should post your onPostExcute code with your logcat.

Upvotes: 1

duynt
duynt

Reputation: 128

Notice that if you want to delete some items while traverse, you should you Iterator. And if your AsynTask was not created on UI thread, make sure you use thread-safe.

Upvotes: 1

Related Questions