Android -- 如何正确处理 onPause/onResume 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2691570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android -- How to properly handle onPause/onResume methods?
提问by RyanM
I have an app that starts playing sounds and begins/resumes gameplay in the onResume() method, but what I'm noticing is that if my app was the last run application when I put the phone into standby (screen off), and I just press the Menu button to check the time, then the phone starts playing the game and sounds in the background (the app isn't actually visible, only the screen with the date/time is, yet onResume must have been called in my app). What am I to do here? Is there a way to discern what is reactivating the app, and then add a conditional statement that only starts the game when the app is actually visible?
我有一个应用程序开始播放声音并在 onResume() 方法中开始/恢复游戏,但我注意到如果我的应用程序是我将手机置于待机状态(屏幕关闭)时最后运行的应用程序,并且我只需按菜单按钮检查时间,然后手机开始玩游戏并在后台发出声音(该应用程序实际上不可见,只有带有日期/时间的屏幕,但必须在我的应用程序中调用了 onResume )。我来这里做什么?有没有办法辨别是什么重新激活了应用程序,然后添加一个条件语句,该语句仅在应用程序实际可见时才启动游戏?
Here is a snippet from my onResume:
这是我的 onResume 的一个片段:
@Override
protected void onResume()
{
mySaveGame = Utilities.loadSavegame(this);
//check the savegame
if(mySaveGame!=null)
{
//start game using savegame values
this.startFromSavedGame(mySaveGame.getIsLevelComplete());
}
else
{
//run the 1st-run components
this.startFirstRun();
}
super.onResume();
}
The only thing I can think of doing to prevent the game from starting whenever the screen gets turned on (even when the app isn't visible) is to put this.finish() as the last line in onPause()... but that forces you to restart the app every time you want to go back to it because the precess itself was killed (which is fine because my onPause saves persistent data, but it's not an elegant solution).
每当屏幕打开时(即使应用程序不可见),我唯一能想到的阻止游戏启动的方法就是将 this.finish() 作为 onPause() 的最后一行......但是这迫使您每次想要返回应用程序时都重新启动应用程序,因为进程本身已被终止(这很好,因为我的 onPause 保存了持久数据,但这不是一个优雅的解决方案)。
Please help.
请帮忙。
采纳答案by CommonsWare
Have you considered switching to onStart()
and onStop()
, rather than onResume()
and onPause()
?
您是否考虑过切换到onStart()
andonStop()
而不是onResume()
and onPause()
?
回答by MichalisB
I was having the same problem (I had my music player resume/pause at onResume()/onPause()
) and the best solution I found is to pause and resume my activity when it is on the foreground which you can get with public void onWindowFocusChanged (boolean hasFocus)
callback.
我遇到了同样的问题(我的音乐播放器在 恢复/暂停onResume()/onPause()
),我发现的最佳解决方案是暂停并恢复我的活动,当它位于前台时,您可以通过public void onWindowFocusChanged (boolean hasFocus)
回调获得 。
Edit: This in an old and slightly incorrect answer - correct response is described in the Android Developers Blog: making android games that play nice
编辑:这是一个旧的且稍微不正确的答案 - Android 开发者博客中描述了正确的答案:使 android 游戏玩起来很好