I have a function function()
. How can I ensure, when the function is called multiple times, that it would run one after another, and not concurrently? I am using Kotlin 1.3 in Android Studio 3.5.
My code is
button.setOnClickListener{function()}
The problem is, the button can be pressed multiple times while the function()
is still running, hence I want the subsequent invocations of function()
to be executed sequentially, one after another, instead of at the same time.
EDIT: My function definition is
fun function(){
image.animate().rotationBy(360F).setDuration(500)
}
The user could press the button more than once, hence the result is that the image(image
) gets rotated to an angle, as the image is being rotated again while it did not complete the full rotation, ending up being rotated at another angle instead of 0°.
You can call this function only from an
IntentService
. The IntentService offloads task from the main thread and runs them over aWorker Thread
. The tasks in Worker Thread are executed sequentially.You can start this service using
startService()
method.