问题描述
onActivityResult让我头疼不已.我的小游戏有标题/菜单屏幕.单击新游戏"后,将启动一个活动,该活动将为我的游戏世界创建一个SurfaceView.
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.
进行游戏时,玩家可以进入游戏世界中的建筑物.进入建筑物后,我从SurfaveView启动该特定建筑物的活动.这是一个示例:
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是在游戏活动onCreate中定义的:
mMinerClass is defined in the game Activity onCreate:
mMap .mMinerClass = Miner.this;
mMap是SurfaceView.我正在传递包含播放器状态的捆绑包.在onActivityResult中,我抓取了商店活动返回的捆绑包并更新了玩家状态.
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稍后用于设置新播放器状态.
mSavedPlayer is later used to set the new player state.
以上所有方法都可以正常使用
我可以进入商店,购买东西,离开,并且播放器已正确更新.
I can enter stores, buy stuff, leave and the player is updated correctly.
问题在我按下主页"按钮(或某些返回按钮)时出现,并且调用了onDestroy.我的onDestroy方法除了调用super.onDestroy()之外什么也不做.
The problem manifests when I press the Home button (or someyimes back button) and onDestroy gets called. My onDestroy method does nothing besides call super.onDestroy();
在调用onDestroy()之后,不再调用onActivityResult.
After onDestroy() gets called onActivityResult no longer gets called.
所以:
1)我玩游戏,一切都很好.
1) I play the game, everthing works great.
2)我按主页,然后将破坏记为
2) I press home and ondestroy is called
3)通过启动器图标重新启动我的游戏
3) relaunch my game via launcher icon
4)恢复游戏.我仍然可以进入和离开商店,但是不再调用onActivityResult.
4) Resume game. I can still enter and leave stores but onActivityResult is no longer called.
这是一些logcat输出.每次输入相关方法时,我都会记录日志.
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!
推荐答案
您应格外注意这一行:
((Activity) mContext).startActivityForResult(storeIntent, Miner.REQUEST_STORE)
您确定mContext是正确的活动吗?我怀疑这不是当前活动,而是对另一个活动实例(之前关闭的第一个活动实例)的引用.您可以使用"this"代替mContext吗?
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?
这篇关于在onDestroy之后不再调用onActivityResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!