Skip to content

Commit

Permalink
Fix crashes where the Android app restored from the background.
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Feb 3, 2024
1 parent 59af243 commit 635002b
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 97 deletions.
16 changes: 11 additions & 5 deletions Android/app/src/main/java/name/jinleili/bevy/BevySurfaceView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.view.SurfaceView
class BevySurfaceView : SurfaceView, SurfaceHolder.Callback2 {
private var rustBrige = RustBridge()
private var bevy_app: Long = Long.MAX_VALUE
private var ndk_inited = false
private var idx: Int = 0
private var sensorManager: SensorManager? = null
private var mSensor: Sensor? = null
Expand Down Expand Up @@ -39,11 +40,16 @@ class BevySurfaceView : SurfaceView, SurfaceHolder.Callback2 {
// 绘制表面被创建后,创建/重新创建 Bevy App
override fun surfaceCreated(holder: SurfaceHolder) {
holder.let { h ->
rustBrige.init_ndk_context(this.context)
if (!ndk_inited) {
ndk_inited = true
rustBrige.init_ndk_context(this.context)
}

// Get the screen's density scale
val scaleFactor: Float = resources.displayMetrics.density
bevy_app = rustBrige.create_bevy_app(this.context.assets, h.surface, scaleFactor)
if (bevy_app == Long.MAX_VALUE) {
// Get the screen's density scale
val scaleFactor: Float = resources.displayMetrics.density
bevy_app = rustBrige.create_bevy_app(this.context.assets, h.surface, scaleFactor)
}

// SurfaceView 默认不会自动开始绘制,setWillNotDraw(false) 用于通知 App 已经准备好开始绘制了。
setWillNotDraw(false)
Expand All @@ -64,7 +70,7 @@ class BevySurfaceView : SurfaceView, SurfaceHolder.Callback2 {
}
}

// 绘制表面被销毁后,也销毁 Bevy App
// 绘制表面被销毁后,也销毁 Bevy 中的 Android window
override fun surfaceDestroyed(holder: SurfaceHolder) {
if (bevy_app != Long.MAX_VALUE) {
rustBrige.release_bevy_app(bevy_app)
Expand Down
2 changes: 1 addition & 1 deletion Android/app/src/main/java/name/jinleili/bevy/RustBridge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class RustBridge {
System.loadLibrary("bevy_in_app")
}

external fun init_ndk_context(ctx: Context,)
external fun init_ndk_context(ctx: Context)
external fun create_bevy_app(asset_manager: AssetManager, surface: Surface, scale_factor: Float): Long
external fun enter_frame(bevy_app: Long)
external fun device_motion(bevy_app: Long, x: Float, y: Float, z: Float)
Expand Down
4 changes: 2 additions & 2 deletions Android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ buildscript {
}
}
plugins {
id 'com.android.application' version '7.4.1' apply false
id 'com.android.library' version '7.4.1' apply false
id 'com.android.application' version '7.4.2' apply false
id 'com.android.library' version '7.4.2' apply false
id 'org.jetbrains.kotlin.android' version '1.7.0' apply false
}

Expand Down
Loading

0 comments on commit 635002b

Please sign in to comment.