Hi there,
i hope you can help me with the following activity-lifecircle problem:
Situation:I have two Activities, one for the Menu and one for the Game.
The GameActivity is started by the MenuActivity via
startActivityForResult(GameIntent) after pressing "start Game"
(see
pic0 number 1. )
There are two different jPCT-Environments within these two Activities, that will be individually cleaned by their
associated
Activity.onDestroy() method when calling
Activity.this.finish().
The user has differnet options after pressing "pause Game":
a. Resume the game
-> simply unpausing the game
b. Quit the game
//pseudo code
GameActivity.this.setResult(RESULT_QUIT);
GameActivity.finish();
MenuActivity.onActivityResult(...){ ... case RESULT_QUIT: MenuActivity.finish() }
c. Back to Main Menu
//pseudo code
GameActivity.this.setResult(RESULT_MAIN_MENU);
GameActivity.finish();
MenuActivity.onActivityResult(...){ ... case RESULT_MAIN_MENU: MenuActivity.currentScreen = Screen.MAIN_MENU; }
d. Restart the game
//pseudo code
GameActivity.this.setResult(RESULT_RESTART);
GameActivity.finish();
MenuActivity.onActivityResult(...){ ... case RESULT_RESTART: startActivityForResult(GameIntent) }
Problem:When restarting the GameActivity immediately after destroying (see option d.), the jpct-World doesn't load (see
pic2),
although the Framebuffer is ready (you can see the key buttons on pic 2).
I think the old GameActivity with the cleaned jPCT-Environment is still on the Activity stack and not
destroyed quickly enough by the garbage collector. (see
pic0 number 2. ) [reason for this argument: see clumsy workaround 1, lower]
Restarting the GameActivity by pressing "start Game" after returning to the Main Menu (see option c.) is working (see
pic1).
pic0short workflow
pic1Display after pressing "start Game"
pic2Display after pressing "restart Game"
Clumsy workaround 1:When returning to MenuActivity with RESULT_RESTART, pasting a
thread.sleep for 5 sec before calling startActivityForResult(GameIntent)
-> result: World will be loaded like shown in
pic1Issue:
5 sec blackscreen appears between activity switching
Clumsy workaround 2:When returning to MenuActivity with RESULT_RESTART, putting a
System.exit(0); in GameActivity.onDestroy() as last line
-> result: World will be loaded like shown in
pic1Issues (
EDIT):
-rough Activity changing with short ugly blackscreen, lumberjack style :S
-a following "back to Menu"-action from GameActivity ends up in a restart of the MenuActivity too,
because System.exit(0) clears the states of all Activities! - i guess
Question:Does anyone know a good way to get rid of old activity instances immediately?
I think about a command like "gc, clean the stack NOW!" (System.gc() doesn't help, still takes 5 sec until the GC get its ash up)
Or is there a way to get the number of the Activities on stack?
I am wondering why no new instance of GameActivity is build after calling
startActivityForResult(GameIntent) immediately?
Maybe I have to set a flag like "the old GameActivty is expired"? Any ideas?
EDIT:
Reset the GameIntent before calling
startActivityForResult(GameIntent) doesn't help neither
[attachment deleted by admin]