I have an API request and I should open new activity or show an error depends on the response.
CoroutineScope(IO).launch {
val result = joinGameWithFriendRequest(inputSessionId)
if (result.error == "Session id not found") {
showToast("Session id not found")
this.cancel() // How to stop here?
}
if (this.isActive)
withContext(Dispatchers.Main) {
val intent = Intent(this@PlayWithFriend, PlayField::class.java)
startActivity(intent) //todo start new Activity in a func
}
}
I found a solution using cancel() and .isActive but it looks bad and if you have multiple consecutive checks, the code will be terrible. Is there some nice way to interrupt the coroutine like "return" in functions?