简体   繁体   English

在onDestroy之后不再调用onActivityResult

[英]onActivityResult no longer being called after onDestroy

onActivityResult is giving me major headaches. onActivityResult让我头疼不已。 My little game has a title / menu screen. 我的小游戏有标题/菜单屏幕。 Upon clicking "new game" an Activity is launched which creates a SurfaceView for my game world. 单击“新游戏”后,将启动一个活动,该活动将为我的游戏世界创建一个SurfaceView。

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. 进入建筑物后,我从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: mMinerClass在游戏Activity onCreate中定义:

  mMap .mMinerClass = Miner.this;

mMap is the SurfaceView. mMap是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. 在onActivityResult中,我获取商店活动返回的包并更新玩家状态。

 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. mSavedPlayer稍后用于设置新的播放器状态。

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. 当我按下“主页”按钮(或某些返回按钮)并调用onDestroy时,问题就会显现。 My onDestroy method does nothing besides call super.onDestroy(); 我的onDestroy方法除了调用super.onDestroy()之外什么也不做。

After onDestroy() gets called onActivityResult no longer gets called. 在onDestroy()被调用之后,onActivityResult将不再被调用。

So: 所以:

1) I play the game, everthing works great. 1)我玩游戏,一切正常。

2) I press home and ondestroy is called 2)我按回家,然后叫毁灭

3) relaunch my game via launcher icon 3)通过启动器图标重新启动我的游戏

4) Resume game. 4)恢复游戏。 I can still enter and leave stores but onActivityResult is no longer called. 我仍然可以进入和离开商店,但不再调用onActivityResult。

Here is some logcat output. 这是一些logcat输出。 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? 您确定mContext是正确的活动吗? 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? 您可以使用“ this”代替mContext吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM