Answer the question
In order to leave comments, you need to log in
How can I make it so that when the fragment is closed, it is no longer accessed?
CoroutineScope(Dispatchers.IO).launch {
while (true) {
val msg = Message()
msg.what = mediaPlayer.currentPosition
handler.sendMessage(msg)
delay(10)
}
}
}
@SuppressLint("HandlerLeak")
var handler = object : Handler(Looper.getMainLooper()) {
override fun handleMessage(msg: Message) {
val currentPosition = msg.what
// Update positionBar
positionBar.progress = currentPosition
// Update Labels
val elapsedTime = createTimeLabel(currentPosition)
elapsedTimeLabel.text = elapsedTime
val remainingTime = createTimeLabel(totalTime - currentPosition)
remainingTimeLabel.text = "-$remainingTime"
}
}
Answer the question
In order to leave comments, you need to log in
Omagad.
Read about structured concurrency. Read about the life cycle of fragments.
Everything happens because you mistreat them. Due to the fact that you have changed one tool for another, the mistreatment has not gone away. You must cancel the scope when leaving the fragment (in onDestroyView). And contact the main thread through MainDispatcher, since you are using coroutines.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question