@@ -7,6 +7,7 @@ import androidx.activity.ComponentActivity
77import androidx.lifecycle.DefaultLifecycleObserver
88import androidx.lifecycle.Lifecycle
99import androidx.lifecycle.LifecycleOwner
10+ import androidx.lifecycle.coroutineScope
1011import com.google.android.filament.Engine
1112import com.google.android.filament.IndirectLight
1213import com.google.android.filament.MaterialInstance
@@ -44,6 +45,9 @@ import io.github.sceneview.model.Model
4445import io.github.sceneview.model.ModelInstance
4546import io.github.sceneview.node.LightNode
4647import io.github.sceneview.node.Node
48+ import kotlinx.coroutines.CoroutineScope
49+ import kotlinx.coroutines.Dispatchers
50+ import kotlinx.coroutines.launch
4751
4852/* *
4953 * A SurfaceView that integrates with ARCore and renders a scene
@@ -561,7 +565,7 @@ open class ARSceneView @JvmOverloads constructor(
561565
562566 override fun destroy () {
563567 if (! isDestroyed) {
564- arCore.destroy ()
568+ destroyARCore ()
565569
566570 defaultCameraNode?.destroy()
567571 defaultCameraStream?.destroy()
@@ -573,6 +577,14 @@ open class ARSceneView @JvmOverloads constructor(
573577 super .destroy()
574578 }
575579
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+
576588 class DefaultARCameraNode (engine : Engine ) : ARCameraNode(engine) {
577589 init {
578590 // Set the exposure on the camera, this exposure follows the sunny f/16 rule
@@ -596,7 +608,7 @@ open class ARSceneView @JvmOverloads constructor(
596608 }
597609
598610 override fun onDestroy (owner : LifecycleOwner ) {
599- arCore.destroy ()
611+ destroyARCore ()
600612 }
601613 }
602614
0 commit comments