I am getting the following exception from the following piece of code sometimes People suggest that if I make the for loop to an iterator, It would fix the issue. But I don't understand If I am not removing any element inside the for loop but calling pendingActions.clear(); later. Please let me know if anyone has come across any of this scenario. Also , Would you think if I make list declaration to
private List<Action> pendingActions = Collections.synchronizedList(new ArrayList<>());
will fix the issue ?
private List<Action> pendingActions = new ArrayList<>();
private void runPendingNavigationActions() {
if (!pendingActions.isEmpty()) {
for (Action r : pendingActions) {
r.run();
}
pendingActions.clear();
}
}
Exception:
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4156)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4250)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.util.ConcurrentModificationException
at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
at com.app.android.view.fragment.MainFragment.runPendingNavigationActions(MainFragment.java:1355)
Actionremove itself from the list in itsrun()method?run()method in yourActionclass hereActions?