【问题标题】:How to disable hardware back button for a specific page only如何仅禁用特定页面的硬件后退按钮
【发布时间】:2020-04-18 18:08:14
【问题描述】:

我遇到了如何禁用后退按钮硬件的问题。我只希望它在我的应用程序的主页上工作,而不是在其他页面上工作。当您在主页上时它运行良好,但也适用于打开的所有其他页面。我希望用户能够在除主页之外的所有页面上使用后退按钮。 这是我的代码

`deactivateBackButton()
this.subscribe = this.platform.backButton.subscribeWithPriority(666666,()=>{
if (this.constructor.name == "HomePage") {
if (window.confirm("Do you want to exit app")) {
navigator["app"].exitApp();
 }
}
});
}`

【问题讨论】:

    标签: android angular ionic-framework


    【解决方案1】:

    我猜你正在尝试实现双击返回按钮两次以退出活动

    boolean doubleBackToExitPressedOnce = false;
    
    @Override
    public void onBackPressed() {
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
            return;
        }
    
        this.doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
    
        new Handler().postDelayed(new Runnable() {
    
            @Override
            public void run() {
                doubleBackToExitPressedOnce=false;                       
            }
        }, 2000);
    }
    

    这是一个按两次后退按钮退出的实现。如果你想要有点相似,你可以根据需要进行修改。

    【讨论】:

    • 我使用的是 IONIC 5,我不太了解 @overid 部分。我只想要当用户登录时我不希望他们能够再次回到他登录页面。当他们点击后退按钮时会发生这种情况
    • 我的建议是完成您不希望用户返回的活动。例如,在您的登录活动中,在您调用 startActivity 之后,立即调用 finish()。当用户点击后退按钮时,他们将无法进入登录活动,因为它已被从堆栈中删除。
    【解决方案2】:

    你可以这样做不要调用 super.onBackPressed()

    @Override
    public void onBackPressed() {
    // super.onBackPressed();
    // Not calling super, disables back button in current screen.
    }
    

    【讨论】:

      【解决方案3】:

      在其他页面上:

      activateBackButton()
          this.subscribe =  this.platform.backButton.subscribeWithPriority(666666,()=>{
                if (this.constructor.name != "HomePage") {
                   // go back to previous page
                }
           });
        }
      

      【讨论】:

      • 让他们试试这个。谢谢
      • 请将其标记为已接受的答案,以便其他人也可以获得帮助。
      • 它成功了,我尝试接受它作为 anwser,但系统不允许我
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-14
      • 2014-05-08
      • 2019-09-16
      • 2021-11-21
      • 1970-01-01
      • 2021-10-24
      相关资源
      最近更新 更多