onBackPressed() 不起作用
我试图通过单击后退按钮来关闭我的应用程序。在主屏幕上,后退按钮会将您带到制作人员屏幕。从那个制作人员名单屏幕上,我想用后退按钮关闭应用程序。不幸的是,我调用的 onBackPressed() 方法似乎根本没有执行。我不知道如何继续。
package app.jonward.boss;
import android.app.Activity;
public class Credits extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.credits);
//Doing stuff
}
@Override
public void onBackPressed() {
finish();
return;
}
}
I'm trying to close my app on a back button click. From the main screen, the back button takes you to a credits screen. From that credits screen, I want to close the app with the back button. Unfortunately, the onBackPressed() method I'm calling doesn't seem to be executed at all. I'm not sure how to proceed.
package app.jonward.boss;
import android.app.Activity;
public class Credits extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.credits);
//Doing stuff
}
@Override
public void onBackPressed() {
finish();
return;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不明白。
onBackPressed()
的作用是调用finish()
。finish()
是一个Activity
方法,可以这么说,没有办法“退出”您的应用程序。也许您真正想要的是在启动Credits
活动的 Activity 上调用finish()
。在其他活动中:I don't get it. What
onBackPressed()
does is to callfinish()
.finish()
is anActivity
method, there's no way to "quit" your app, so to speak. Perhaps what you really want is to callfinish()
on the Activity that is starting theCredits
activity. Inside the other activity:尝试以下解决方案?
Try the following solution?
就在最近,我遇到了这个问题,我通过删除
build
目录中的所有文件夹来解决它。我不知道怎么做,但似乎
Android Studio
在生成构建时出错并导致了此问题。因此,删除build
目录后,我的onBackpressed()
方法被调用。Just recently, i ran into this issue and i was able to solve it by deleting all the folders in the
build
directory.I don't know how but it seemed that
Android Studio
made an error generating the build and that lead to this problem. So after deleting thebuild
directory, myonBackpressed()
method was being called.抱歉,我还无法发表评论(我希望这是对 Muffinbubble 的答案的评论)..
尝试将 onKeyDown 放入您的主类中,并验证即使您在第二个 Activity 中,是否会调用此 get 。
然后,您可以通过主类检查您所在的位置,并在需要时退出应用程序,否则返回 super.onKeyDown(..)。
Sorry, i am not able to comment yet (I wanted this to be a comment on Muffinbubble's answer)..
Try putting the onKeyDown into your mainclass and validate if this get's called even if you are in your second Activity.
You could then check where you are via the main class and exit the application if needed and else return super.onKeyDown(..).
我不知道您是否找到了答案,但是您是否在重写方法中尝试过 super.onBackPressed ?
如果我是你,我会尝试:
I don't know if you have found an answer yet, but have you tried super.onBackPressed from within your overriding method?
If I were you, I would try: