onDestroy 之后不再调用 onActivityResult
onActivityResult 让我很头疼。我的小游戏有一个标题/菜单屏幕。单击“新游戏”后,将启动一个 Activity,为我的游戏世界创建一个 SurfaceView。
在玩游戏时,玩家可以进入游戏世界中的建筑物。进入建筑物后,我从 SurfaveView 启动该特定建筑物的活动。下面是一个例子:
Intent storeIntent = new Intent(mMinerClass, GeneralStore.class);
storeIntent.putExtra("player", mPlayer.save());
((Activity) mContext).startActivityForResult(storeIntent, Miner.REQUEST_STORE);
mMinerClass是在游戏Activity的onCreate中定义的:
mMap .mMinerClass = Miner.this;
mMap是SurfaceView。 我正在传递一个包含玩家状态的包。在 onActivityResult 中,我获取商店活动返回的包并更新播放器状态。
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Bundle player = intent.getExtras();
mMap.mSavedPlayer = player;
}
mSavedPlayer 稍后用于设置新的播放器状态。
以上所有内容通常都有效
我可以进入商店,购买东西,离开,并且播放器已正确更新。
当我按下主页按钮(或有时返回按钮)并且 onDestroy 被调用时,问题就会出现。我的 onDestroy 方法除了调用 super.onDestroy(); 之外什么也不做
调用 onDestroy() 后,不再调用 onActivityResult。
所以:
1)我玩游戏,一切都很好。
2) 我按 home 键,ondestroy 被调用
3) 通过启动器图标重新启动我的游戏
4) 恢复游戏。我仍然可以进出商店,但 onActivityResult 不再被调用。
这是一些 logcat 输出。每次输入相关方法时我都会记录。
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
我是通过手机发布此内容的,所以请原谅任何愚蠢的拼写错误。
我很感谢你们对此提供的任何见解。谢谢!
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 back 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该额外注意这一行:
您确定 mContext 是正确的活动吗?我怀疑这不是当前活动,而是对另一个活动实例的引用,即之前关闭的第一个活动实例。可以用“this”代替mContext吗?
You should put additional attention to this line:
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?