防止后退按钮关闭我的应用程序

发布于 2024-11-17 18:10:00 字数 486 浏览 0 评论 0原文

我在应用程序的活动中使用以下代码来防止它关闭我的应用程序。

/* Prevent app from being killed on back */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        // Back?
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // Back
            moveTaskToBack(true);
        }

        // Return
        return super.onKeyDown(keyCode, event);

    }

它不起作用。该应用程序设置为兼容 Android 1.6(API 级别 4)。单击我的应用程序图标会在 Splash 活动(这是主活动)中重新启动我的应用程序。如何防止我的应用程序关闭?

I am using the following code in my application's activity to prevent it from closing my app on back.

/* Prevent app from being killed on back */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        // Back?
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // Back
            moveTaskToBack(true);
        }

        // Return
        return super.onKeyDown(keyCode, event);

    }

It does not work. The app is set to be Android 1.6 (API Level 4) compatible. Clicking on my application icon restarts my app at a Splash activity (which is the Main). How can I prevent my app from closing on back?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

凌乱心跳 2024-11-24 18:10:00

更简洁的解决方案:-

@Override
public void onBackPressed() {
    // do nothing. We want to force user to stay in this activity and not drop out.
}

A more succinct solution:-

@Override
public void onBackPressed() {
    // do nothing. We want to force user to stay in this activity and not drop out.
}
笔芯 2024-11-24 18:10:00

您是否尝试过将 super 调用放在 else 块中,以便仅在键不是 KEYCODE_BACK 时才调用它?

/* Prevent app from being killed on back */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        // Back?
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // Back
            moveTaskToBack(true);
            return true;
        }
        else {
            // Return
            return super.onKeyDown(keyCode, event);
        }
    }

但老实说,您不能依赖这一点,因为一旦您的应用程序被置于后台,系统随时可以回收它以回收内存。

Have you tried putting the super call in an else block so it is only called if the key is not KEYCODE_BACK ?

/* Prevent app from being killed on back */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        // Back?
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // Back
            moveTaskToBack(true);
            return true;
        }
        else {
            // Return
            return super.onKeyDown(keyCode, event);
        }
    }

In all honesty though, you can't rely on this because once your app is placed in the background, at any moment it could be recycled for the system to reclaim memory.

烟凡古楼 2024-11-24 18:10:00

即使你可以这样做,你也不应该这样做。强制用户始终将您的应用程序保留在内存中并不是一个好主意,而且只会惹恼他们。

Even if you could do that, you should not. Enforcing users to keep your app in the memory all the time is not a good idea and will only annoy them.

淡水深流 2024-11-24 18:10:00

如果需要向后导航并防止关闭,则在 Android WebView 中使用:

@Override
public void onBackPressed() {
    if (mWebView.canGoBack()) {
        mWebView.goBack();
        return;
    }

    // Otherwise defer to system default behavior.
    super.onBackPressed();
}

If need to navigate back as well as preventing from closing, then in Android WebView use this:

@Override
public void onBackPressed() {
    if (mWebView.canGoBack()) {
        mWebView.goBack();
        return;
    }

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