onActivityResult is giving me major headaches. My little game has a title / menu screen. Upon clicking "new game" an Activity is launched which creates a SurfaceView for my game world.
When playing, the player can enter buildings in the game world. Upon entering a building I launch the activity for that particular building from my SurfaveView. Here is an example:
Intent storeIntent = new Intent(mMinerClass, GeneralStore.class);
storeIntent.putExtra("player", mPlayer.save());
((Activity) mContext).startActivityForResult(storeIntent, Miner.REQUEST_STORE);
mMinerClass is defined in the game Activity onCreate:
mMap .mMinerClass = Miner.this;
mMap is the SurfaceView. I'm passing a bundle containing the state of the player. In onActivityResult I grab the bundle returned by the store activity and update the player state.
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Bundle player = intent.getExtras();
mMap.mSavedPlayer = player;
}
mSavedPlayer is later used to set the new player state.
All the above works generally
I can enter stores, buy stuff, leave and the player is updated correctly.
The problem manifests when I press the Home button (or someyimes ba开发者_JS百科ck button) and onDestroy gets called. My onDestroy method does nothing besides call super.onDestroy();
After onDestroy() gets called onActivityResult no longer gets called.
So:
1) I play the game, everthing works great.
2) I press home and ondestroy is called
3) relaunch my game via launcher icon
4) Resume game. I can still enter and leave stores but onActivityResult is no longer called.
Here is some logcat output. I'm logging each time I enter a relevant method.
App start. Resuming a saved game
07-03 00:33:04.759: ERROR/Miner(4014): -->onCreate
07-03 00:33:04.819: ERROR/Miner(4014): -->onStart
07-03 00:33:08.329: ERROR/Miner(4014): -->onResume
Enter store and buy stuff 2 times. Everything working great.
07-03 00:33:30.419: ERROR/Miner(4014): -->onPause
07-03 00:33:31.279: ERROR/Miner(4014): -->onStop
07-03 00:33:35.069: ERROR/Miner(4014): -->onActivityResult
07-03 00:33:35.069: ERROR/Miner(4014): -->onRestart
07-03 00:33:35.069: ERROR/Miner(4014): -->onStart
07-03 00:33:36.709: ERROR/Miner(4014): -->onResume
07-03 00:33:42.129: ERROR/Miner(4014): -->onPause
07-03 00:33:43.289: ERROR/Miner(4014): -->onStop
07-03 00:33:55.279: ERROR/Miner(4014): -->onActivityResult
07-03 00:33:55.279: ERROR/Miner(4014): -->onRestart
07-03 00:33:55.279: ERROR/Miner(4014): -->onStart
07-03 00:33:56.879: ERROR/Miner(4014): -->onResume
Back button pressed bringing me to title screen
07-03 00:35:26.283: ERROR/Miner(4014): -->onPause
07-03 00:35:27.153: ERROR/Miner(4014): -->onStop
07-03 00:35:27.333: ERROR/Miner(4014): -->onDestroy
Resume Game
07-03 00:36:12.953: ERROR/Miner(4014): -->onCreate
07-03 00:36:13.003: ERROR/Miner(4014): -->onStart
07-03 00:36:14.663: ERROR/Miner(4014): -->onResume
Enter store and buy stuff twice. No more onActivityResult
07-03 00:36:52.063: ERROR/Miner(4014): -->onPause
07-03 00:36:52.863: ERROR/Miner(4014): -->onStop
07-03 00:36:59.913: ERROR/Miner(4014): -->onRestart
07-03 00:36:59.913: ERROR/Miner(4014): -->onStart
07-03 00:37:01.593: ERROR/Miner(4014): -->onResume
07-03 00:37:23.353: ERROR/Miner(4014): -->onPause
07-03 00:37:24.173: ERROR/Miner(4014): -->onStop
07-03 00:37:29.173: ERROR/Miner(4014): -->onRestart
07-03 00:37:29.173: ERROR/Miner(4014): -->onStart
07-03 00:37:30.793: ERROR/Miner(4014): -->onResume
I'm posting this from my phone so excuse any dumb typos.
I appreciate any insight y'all can provide regarding this. Thanks!
You should put additional attention to this line:
((Activity) mContext).startActivityForResult(storeIntent, Miner.REQUEST_STORE)
Are you sure mContext is the correct activity? I suspect it's not the current activity but the reference to another activity instance, the first one that was closed before. Can you use "this" instead of mContext?
精彩评论