From baa7044fd170cca864089bf476140c6fe1b36c31 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Tue, 8 Sep 2020 19:30:56 +0300 Subject: [PATCH] Resolved #3048 - Fixed ANRs on 'Resume' on huge save files --- core/src/com/unciv/MainMenuScreen.kt | 29 +++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/core/src/com/unciv/MainMenuScreen.kt b/core/src/com/unciv/MainMenuScreen.kt index 462b26b0dc..618625d4f7 100644 --- a/core/src/com/unciv/MainMenuScreen.kt +++ b/core/src/com/unciv/MainMenuScreen.kt @@ -7,6 +7,7 @@ import com.badlogic.gdx.scenes.scene2d.actions.Actions import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.utils.Align +import com.unciv.logic.GameInfo import com.unciv.logic.GameSaver import com.unciv.logic.GameStarter import com.unciv.logic.map.mapgenerator.MapGenerator @@ -165,15 +166,25 @@ class MainMenuScreen: CameraStageBaseScreen() { private fun autoLoadGame() { - try { - game.loadGame(autosave) - dispose() - } - catch (outOfMemory:OutOfMemoryError){ - ResponsePopup("Not enough memory on phone to load game!", this) - } - catch (ex: Exception) { // silent fail if we can't read the autosave - ResponsePopup("Cannot resume game!", this) + ResponsePopup("Loading...", this) + thread { // Load game from file to class on separate thread to avoid ANR... + val savedGame: GameInfo + try { + savedGame = GameSaver.loadGameByName(autosave) + } catch (outOfMemory: OutOfMemoryError) { + ResponsePopup("Not enough memory on phone to load game!", this) + return@thread + } + + Gdx.app.postRunnable { /// ... and load it into the screen on main thread for GL context + try { + game.loadGame(savedGame) + dispose() + } catch (ex: Exception) { // silent fail if we can't read the autosave + ResponsePopup("Cannot resume game!", this) + } + + } } }