如何关闭应用程序?
我有一个启动屏幕,其中有一些代码我只想调用一次,因此在显示启动屏幕后,它会转到我的主游戏,并且我不希望它再次返回启动屏幕,直到游戏再次启动。但是当我返回主屏幕时,它又回到初始屏幕。当从主屏幕按下后退按钮而不是返回到初始屏幕时,如何使游戏关闭?
这是我现在在主游戏中所拥有的内容,但它仍然可以追溯到飞溅......
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Toast.makeText(getApplicationContext(),"back was pressed",Toast.LENGTH_LONG).show();
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
i have a splash screen that has some code that i only want called once so after showing the splash screen it goes to my main game, and i don't want it going back to the splash screen again until the game is launched again. but when i hit back on my main screen, it goes back to the splash screen. how do i make the game close when the back button is pressed from the main screen instead of going back to the splash screen?
here is what i have right now on my main game, but it still goes back to the splash...
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Toast.makeText(getApplicationContext(),"back was pressed",Toast.LENGTH_LONG).show();
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在执行
startActivity()
后立即在启动活动中调用finish()
:)Call
finish()
in the splash activity right after you dostartActivity()
:)如果您要启动活动,则可以使用 FLAG_ACTIVITY_NO_HISTORY。请参阅此以获取参考。我相信如果这是您的初始活动,您也可以以某种方式做同样的事情。
If you are starting the Activity you can use FLAG_ACTIVITY_NO_HISTORY. See this for a reference. I'm sure you can do the same somehow if it is your initial activity.
您可以在 Splash 活动的 onResume 方法中调用
finish()
。杰克的解决方案可能是最好的。You could put a call to
finish()
in your onResume method for the Splash activity. Jack's solution is probably best though.