androidandroid-activitykotlin-coroutinesandroid-lifecycle

OnResume called in quick succession


My app has a crash happening on production, the cause is due to a java.util.ConcurrentModificationException.

In the Breadcrump of the crash log, I noticed that the activities's onResume was called twice in quick succession(within dozens or at most about 300milliseconds), like onResume->onPause->onResume. My app starts a co-routine using the IO dispatcher in the onResume, this co-routine operates on a list, when the onResume is called in quick succession, 2 threads are launched, and they access the same list, causing the crash.

Anyone has any idea when the onResume can be called in such quick succession? It is very unlikely users can do that so quickly. I do notice in many of the cases, the crash happened around screen waking up, but not all of them.


Solution

  • After some analysis, this can be an edge case that when the onResume is called by the app while the activity is turning active, some other app or dialog can be shown, then goes away, causing the onResume being called 2 times. I added code to use synchronized to prevent the list being concurrently accessed, which fixed the bug.