Cancels all children jobs of this coroutine using Job.cancel for all of them. Unlike Job.cancel on this job as a whole, the state of this job itself is not affected.
If you cancel the job then you have cancelled the whole coroutine scope, so launching a new task in that scope does not work, use this only in onDestroy.
If you cancel children then you scope remain alive, so you can launch new tasks.
You can consider to keep a reference for the single setState job and cancel only it.
private var setStateJob: Job? = null
private fun setState(state: State) {
setStateJob?.cancel() // cancelAndJoin ?
setStateJob = launch {
Log.d("MyFragment", "before delay")
delay(500)
Log.d("MyFragment", "after delay")
// do something with state
}
}
i have called job.cancel() in onDestroyView and then e.g. orientation change will cause job to be canceled but somehow it still keeps working fine.
problem happens if job.cancel() is called before atleast once launch {} is called
keeping a reference for the single setState job works but then i guess it looses whole point of inheriting from CoroutineScope if i need to keep reference to every single job i create in this fragment