[英]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 device
的hardware 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.