简体   繁体   English

硬件后退按钮关闭启用Tab的Ionic 3应用程序上的应用程序

[英]Hardware back button closes the app on Tab enabled Ionic 3 app

When the user hits my profile page(or tab), the user is not logged in then it shows login page as shown below.On the login page where it has header back button and it is working fine (you can see the code below).I need the same functionality when the user hits the hardware back button on Android device .But it closes the app.Can you tell me how to solve this issue? 当用户点击my profile页面(或标签页)时,用户没有登录,然后显示login page ,如下所示。在login page ,它有header back button ,它工作正常(你可以看到下面的代码)当用户点击hardware back button on Android devicehardware back button on Android device时,我需要相同的功能。但它关闭了应用程序。你能告诉我如何解决这个问题吗? If you need more info please let me know. 如果您需要更多信息,请告诉我。

my-profile.ts 我-profile.ts

constructor(public navCtrl: NavController, public app: App, public userService: UserService
        ) {
        if (!this.userService.userDetails) {
            this.app.getRootNav().setRoot('Login', { profile: true });
            return;
        }
    }

login.ts login.ts

constructor(public navCtrl: NavController, public navParams: NavParams) {
        this.profile = this.navParams.data.profile;
    }

    back() {
        if (this.profile) {//not logged in user
            this.navCtrl.setRoot('TabsPage');
        }
    }

login.html 的login.html

<ion-header>
  <ion-navbar>
    <ion-buttons left>
      <button ion-button *ngIf="profile" (click)="back()" tappable><ion-icon name="arrow-back"></ion-icon></button>
    </ion-buttons>
    <ion-title>login</ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>

</ion-content>

You can use the Platform's registerBackButtonAction() method to handle when the user press the native back button. 您可以使用Platform的registerBackButtonAction()方法来处理用户按下本机后退按钮的时间。

import { Platform } from 'ionic-native';
...
constructor(public platform: Platform){

  platform.registerBackButtonAction(() => {
    if (navCtrl.canGoBack()) { // CHECK IF THE USER IS IN THE ROOT PAGE.
      navCtrl.pop(); // IF IT'S NOT THE ROOT, POP A PAGE.
    } else {
      platform.exitApp(); // IF IT'S THE ROOT, EXIT THE APP.
    }
  });

}

But the case here is that you're setting roots and not pushing pages, so this is the normal behaviour of Ionic, when in root page and there's nothing to pop to, the app is closed. 但是这里的情况是你正在设置root而不是推送页面,所以这是Ionic的正常行为,当在root页面中没有任何内容可以弹出时,应用程序将被关闭。

In this case you can do something like this on your login.ts 在这种情况下,您可以在login.ts上执行类似的操作

platform.registerBackButtonAction(() => {
  this.back();
});

So every time the user is in the login page and hits the hardware back button it simply call your back() method that'll set the root to another page. 因此,每当用户进入登录页面并点击硬件后退按钮时,它只需调用您的back()方法,即将根设置为另一个页面。

Hope this helps. 希望这可以帮助。

Just set the property navExitApp to false in file app.module.ts at imports 只需在导入的app.module.ts文件app.module.ts属性navExitApp设置为false app.module.ts

IonicModule.forRoot(YourApp, {
  navExitApp: false
}),

Easy! 简单! You do not need to touch the registerBackButtonAction event. 您无需触摸registerBackButtonAction事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 离子防止硬件后退按钮退出应用程序 - Ionic prevent exit app on hardware back button 单击消息应用程序的硬件后退按钮后,离子返回触发器 - Ionic go back trigger on click of hardware back button of message app 如何防止 android 中的硬件后退按钮关闭 Ionic 3 应用程序? - How to prevent Ionic 3 app close by hardware back button in android? 使用 Ionic 4 的 Android 上的应用程序没有硬件后退按钮 - Hardware back button doesn't the app on Android with Ionic 4 离子电容器硬件后退按钮自动关闭应用程序 - Ionic Capacitor hardware back button is automatically closing the app 为什么在父页面上按下时,Android硬件后退按钮会关闭Ionic V3基本页面(选项卡控制器)? - Why Android hardware-back-button closes Ionic V3 base page (tab controller) when pressed on parent-page? 单击硬件后退按钮,Ionic-5 应用程序正在关闭而不是返回上一页 - Ionic-5 app is closing instead back to previous page on click of hardware back button React-Native BackHandler始终在Android中关闭App(硬件返回) - React-Native BackHandler always closes App in Android (hardware back) 在 ionic 5 android 中按下后退按钮时应用程序关闭,而模态打开 - App closes when the back button is pressed in ionic 5 android while the modal is opened 按下后退按钮时,Android应用程序关闭 - Android app closes when back button pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM