@@ -7,6 +7,7 @@ import androidx.activity.ComponentActivity
7
7
import androidx.lifecycle.DefaultLifecycleObserver
8
8
import androidx.lifecycle.Lifecycle
9
9
import androidx.lifecycle.LifecycleOwner
10
+ import androidx.lifecycle.coroutineScope
10
11
import com.google.android.filament.Engine
11
12
import com.google.android.filament.IndirectLight
12
13
import com.google.android.filament.MaterialInstance
@@ -44,6 +45,9 @@ import io.github.sceneview.model.Model
44
45
import io.github.sceneview.model.ModelInstance
45
46
import io.github.sceneview.node.LightNode
46
47
import io.github.sceneview.node.Node
48
+ import kotlinx.coroutines.CoroutineScope
49
+ import kotlinx.coroutines.Dispatchers
50
+ import kotlinx.coroutines.launch
47
51
48
52
/* *
49
53
* A SurfaceView that integrates with ARCore and renders a scene
@@ -561,7 +565,7 @@ open class ARSceneView @JvmOverloads constructor(
561
565
562
566
override fun destroy () {
563
567
if (! isDestroyed) {
564
- arCore.destroy ()
568
+ destroyARCore ()
565
569
566
570
defaultCameraNode?.destroy()
567
571
defaultCameraStream?.destroy()
@@ -573,6 +577,14 @@ open class ARSceneView @JvmOverloads constructor(
573
577
super .destroy()
574
578
}
575
579
580
+ fun destroyARCore () {
581
+ val scope = lifecycle?.coroutineScope ? : CoroutineScope (Dispatchers .IO )
582
+ scope.launch(Dispatchers .IO ) {
583
+ // destroy should be called off the main thread since it hangs for many seconds
584
+ arCore.destroy()
585
+ }
586
+ }
587
+
576
588
class DefaultARCameraNode (engine : Engine ) : ARCameraNode(engine) {
577
589
init {
578
590
// Set the exposure on the camera, this exposure follows the sunny f/16 rule
@@ -596,7 +608,7 @@ open class ARSceneView @JvmOverloads constructor(
596
608
}
597
609
598
610
override fun onDestroy (owner : LifecycleOwner ) {
599
- arCore.destroy ()
611
+ destroyARCore ()
600
612
}
601
613
}
602
614
0 commit comments