Ionic 4 android硬件后退按钮和工具栏后退不能在同一流程中使用

问题描述 投票:0回答:1

我在使用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);
            }
          }
        });
      }
}

谢谢。

android ionic-framework ionic4 back-button
1个回答
0
投票
我认为,this.navCtrl.navigateBack(url);像假设行为是异步的那样再次推送页面。

为什么不弹出导航页面,假设您具有通用功能

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

并且只需从导航页面组件中调用,该组件必须具有构造函数(公共navCtrl:NavController,公共平台:Platform){},然后在同一导航页面组件中定义并调用通用方法BackButton(this.platform,this.navCtrl)。
© www.soinside.com 2019 - 2024. All rights reserved.