如何捕获按下主页按钮?

发布于 2024-12-18 03:13:24 字数 68 浏览 4 评论 0原文

在我的应用程序中,如果用户单击主页按钮但也想维护活动堆栈,我想打开登录屏幕。

有什么办法可以做到这一点吗?

In my application, I would like to open login screen if user click home button but also want to maintain activity stack.

Is there any way to do this?

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

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

发布评论

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

评论(3

述情 2024-12-25 03:13:24

没有直接的方法可以做到同样的事情。就像您无法获取 HOME 键的 KeyDown 事件一样。但我们可以通过以下代码捕获 HOME 键并保持在同一屏幕中

@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);

    super.onAttachedToWindow();
}

There is no direct way to do the same. like you can not get the KeyDown event for the HOME key. but we can get the HOME key captured and stay in the same screen by following code

@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);

    super.onAttachedToWindow();
}
爱你是孤单的心事 2024-12-25 03:13:24

实际上你应该覆盖主页按钮:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {

    Intent new(this, login);
    start(intent);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Actually you should override to home button:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {

    Intent new(this, login);
    start(intent);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
爱殇璃 2024-12-25 03:13:24

出于安全原因,您绝对无法捕获主页按钮单击事件。

但是,您可以通过重写方法 onNewIntent() 来执行一些操作,以及按 Home 按钮执行的操作。

You cannot capture Home button click event definitely for security reasons.

However, you can do something along with actions taken by pressing Home button by overriding method onNewIntent().

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