onBackPressed() 不起作用

发布于 2024-11-18 10:35:54 字数 476 浏览 4 评论 0原文

我试图通过单击后退按钮来关闭我的应用程序。在主屏幕上,后退按钮会将您带到制作人员屏幕。从那个制作人员名单屏幕上,我想用后退按钮关闭应用程序。不幸的是,我调用的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

几味少女 2024-11-25 10:35:54

我不明白。 onBackPressed() 的作用是调用 finish()finish() 是一个 Activity 方法,可以这么说,没有办法“退出”您的应用程序。也许您真正想要的是在启动 Credits 活动的 Activity 上调用 finish() 。在其他活动中:

   finish();
   startActivity(this, Credits.class);

I don't get it. What onBackPressed() does is to call finish(). finish() is an Activity method, there's no way to "quit" your app, so to speak. Perhaps what you really want is to call finish() on the Activity that is starting the Credits activity. Inside the other activity:

   finish();
   startActivity(this, Credits.class);
若相惜即相离 2024-11-25 10:35:54

尝试以下解决方案?

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        finish();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Try the following solution?

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        finish();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
疯了 2024-11-25 10:35:54

就在最近,我遇到了这个问题,我通过删除 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.

Snapshot of which directory to delete

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 the build directory, myonBackpressed() method was being called.

忆离笙 2024-11-25 10:35:54

抱歉,我还无法发表评论(我希望这是对 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(..).

痕至 2024-11-25 10:35:54

我不知道您是否找到了答案,但是您是否在重写方法中尝试过 super.onBackPressed ?

如果我是你,我会尝试:

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}

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:

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文