Merge "Also cancel TaskView collection Jobs when Overview is not on screen." into main

This commit is contained in:
Treehugger Robot
2025-03-25 10:37:01 -07:00
committed by Android (Google) Code Review
2 changed files with 15 additions and 10 deletions
@@ -2771,6 +2771,7 @@ public abstract class RecentsView<
}
if (enableRefactorTaskThumbnail()) {
mRecentsViewModel.onReset();
getTaskViews().forEach(TaskView::cancelJobs);
}
}
@@ -892,17 +892,21 @@ constructor(
return thumbnailPosition
}
override fun onDetachedFromWindow() =
traceSection("TaskView.onDetachedFromWindow") {
super.onDetachedFromWindow()
if (enableRefactorTaskThumbnail()) {
// The jobs are being cancelled in the background thread. So we make a copy of the
// list to prevent cleaning a new job that might be added to this list during
// onAttach or another moment in the lifecycle.
val coroutineJobsToCancel = coroutineJobs.toList()
coroutineJobs.clear()
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
cancelJobs()
}
fun cancelJobs() =
traceSection("TaskView.cancelJobs") {
// The jobs are being cancelled in the background thread. So we make a copy of the
// list to prevent cleaning a new job that might be added to this list during
// onAttach or another moment in the lifecycle.
val coroutineJobsToCancel = coroutineJobs.toList()
coroutineJobs.clear()
if (coroutineJobsToCancel.isNotEmpty()) {
coroutineScope.launch(dispatcherProvider.background) {
traceSection("TaskView.onDetachedFromWindow.cancellingJobs") {
traceSection("TaskView.cancelJobs.cancellingJobs") {
coroutineJobsToCancel.forEach {
it.cancel("TaskView detaching from window")
}