Fixed a bug where auto-save upon exiting the game could trigger application exit.

This commit is contained in:
Wei Liu 2025-09-21 21:14:48 +08:00
parent baeea1239d
commit ac4d0332c7
3 changed files with 36 additions and 7 deletions

View File

@ -365,13 +365,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
.setPositiveButton(android.R.string.ok) { _: DialogInterface?, _: Int ->
// Auto save before closing if enabled and emulation is running
if (BooleanSetting.AUTO_SAVE_ON_EXIT.boolean && NativeLibrary.isRunning()) {
emulationState.unpause()
NativeLibrary.saveState(NativeLibrary.AUTO_SAVE_SLOT)
Toast.makeText(requireContext(), R.string.game_saved, Toast.LENGTH_SHORT).show()
// Delay closing to allow save to complete
Handler(Looper.getMainLooper()).postDelayed({
EmulationLifecycleUtil.closeGame()
}, 500)
performAutoSaveAndClose()
} else {
EmulationLifecycleUtil.closeGame()
}
@ -1551,6 +1545,39 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
}
}
private fun performAutoSaveAndClose() {
// Show saving toast immediately
Toast.makeText(requireContext(), R.string.saving, Toast.LENGTH_SHORT).show()
// Perform auto save on background thread to avoid ANR
Thread {
try {
// Ensure emulation is unpaused for saving
emulationState.unpause()
// Wait a bit for emulation to resume
Thread.sleep(200)
// Perform the save
NativeLibrary.saveState(NativeLibrary.AUTO_SAVE_SLOT)
// Switch back to main thread for UI operations
Handler(Looper.getMainLooper()).post {
Toast.makeText(requireContext(), R.string.game_saved, Toast.LENGTH_SHORT).show()
// Delay closing to ensure save is complete and user sees the toast
Handler(Looper.getMainLooper()).postDelayed({
EmulationLifecycleUtil.closeGame()
}, 800)
}
} catch (e: Exception) {
// If save fails, still close the game
Handler(Looper.getMainLooper()).post {
Toast.makeText(requireContext(), R.string.save_failed, Toast.LENGTH_SHORT).show()
// Still close the game even if save failed
Handler(Looper.getMainLooper()).postDelayed({
EmulationLifecycleUtil.closeGame()
}, 1000)
}
}
}.start()
}
companion object {
private val perfStatsUpdateHandler = Handler(Looper.myLooper()!!)
}

View File

@ -874,6 +874,7 @@
<string name="emulation_occupied_autosave_slot">自动保存 - %1$tF %1$tR</string>
<string name="quickload_not_found">没有可用的快速保存。</string>
<string name="game_saved">游戏已保存</string>
<string name="save_failed">保存失败</string>
<string name="auto_save_on_exit">退出时自动保存</string>
<string name="auto_save_on_exit_description">离开应用或关闭游戏时自动保存游戏进度</string>

View File

@ -894,6 +894,7 @@
<string name="emulation_occupied_autosave_slot">Auto Save - %1$tF %1$tR</string>
<string name="quickload_not_found">No Quick Save available.</string>
<string name="game_saved">Game saved</string>
<string name="save_failed">Save failed</string>
<string name="auto_save_on_exit">Auto save on exit</string>
<string name="auto_save_on_exit_description">Automatically save the game when leaving the app or closing the game</string>