diff --git a/sys/android/forkfront/src/com/tbd/forkfront/ForkFront.java b/sys/android/forkfront/src/com/tbd/forkfront/ForkFront.java index 2affc2cee..5a03de68a 100644 --- a/sys/android/forkfront/src/com/tbd/forkfront/ForkFront.java +++ b/sys/android/forkfront/src/com/tbd/forkfront/ForkFront.java @@ -115,7 +115,7 @@ public String decode(byte[] bytes) { }; nhState = new NH_State(this, decoder); - new UpdateAssets(this, onAssetsReady).execute((Void[])null); + new UpdateAssets(this, assetsReadyFor(nhState)).execute((Void[])null); } else { @@ -195,20 +195,29 @@ public void onConfigurationChanged(Configuration newConfig) } // ____________________________________________________________________________________ - private UpdateAssets.Listener onAssetsReady = new UpdateAssets.Listener() + // If the activity is recreated while the assets are being copied, the old copy finishes + // after a new NH_State has replaced this one. Only the copy started for the current state + // may start the game, or the game thread would be started twice. + private UpdateAssets.Listener assetsReadyFor(final NH_State state) { - @Override - public void onAssetsReady(File path) + return new UpdateAssets.Listener() { - // Create save directory if it doesn't exist - File nhSaveDir = new File(path, "save"); - if(!nhSaveDir.exists()) - nhSaveDir.mkdir(); + @Override + public void onAssetsReady(File path) + { + if(state != nhState) + return; - PreferenceManager.setDefaultValues(ForkFront.this, R.xml.preferences, false); - nhState.startNetHack(path.getAbsolutePath()); - } - }; + // Create save directory if it doesn't exist + File nhSaveDir = new File(path, "save"); + if(!nhSaveDir.exists()) + nhSaveDir.mkdir(); + + PreferenceManager.setDefaultValues(ForkFront.this, R.xml.preferences, false); + state.startNetHack(path.getAbsolutePath()); + } + }; + } // ____________________________________________________________________________________ @Override