Ionic 4安卓硬件后退按钮和工具栏后退不在同一流程中工作



我在使用ionic 4开发的android应用程序中遇到了一个问题。我的场景是,如果我浏览页面并使用工具栏后退按钮返回,然后再次导航到另一个页面并使用android硬件后退按钮返回。然后,当使用硬件后退按钮时,我使用工具栏后退键返回的页面显示在中间。

页面A->B->C,然后在工具栏上返回按钮C->B->A

再次页面导航A->D->E,然后使用硬件后退按钮页面导航E->D->B->C->B->A

无法维持状态。

我的代码

在app.component.ts构造函数中

this.router.events.subscribe(event => {
const url = this.router.url //current url
if (event instanceof NavigationEnd) {
const isCurrentUrlSaved = this.navLinksArray.find((item) => { return item === url });
if (!isCurrentUrlSaved) this.navLinksArray.push(url);
}
}); 

ngAfterViewInit() {
this.backButtonSubscription = this.platform.backButton.subscribe(() => {
if (this.router.url === '/tabs/home') {
navigator['app'].exitApp();
} else {
if (this.navLinksArray.length > 1) {
this.navLinksArray.pop();
const index = this.navLinksArray.length - 1;
const url = this.navLinksArray[index];
this.navCtrl.navigateBack(url);
}
}
});
}
}

提前谢谢。

我认为,this.navCtrl.navigationBack(url(;正在再次推送页面,就像行为是异步的一样。

你为什么不直接弹出导航页面,比如说你有一个通用功能

ngAfterViewInit() {
static BackButton(platform:Platform,navCtrl:NavController){
platform.registerBackButtonAction(() => {
navCtrl.pop();
}, 0)
}
}

并且从导航页面组件只调用,它必须具有构造函数(public navCtrl:NavController,public platform:platform({},在同一导航页面组件中定义并调用泛型方法BackButton(this.platform,this.navCtrl(。

最新更新