简体   繁体   English

Ionic 4:硬件返回按钮重新加载应用程序

[英]Ionic 4: Hardware Back Button Reloading Application

Working on a Project and stuck in an Issue:在一个项目上工作并陷入一个问题:

Hardware Back Button Reloading Application (I am using Angular Router in this application).硬件后退按钮重新加载应用程序(我在此应用程序中使用 Angular Router)。

My Code to Exit Application:我退出应用程序的代码:

  ionViewDidEnter(){
      this.subscription = this.platform.backButton.subscribe(()=>{
          navigator['app'].exitApp();
      });
  }

  ionViewWillLeave(){
        this.subscription.unsubscribe();
  }

While same logic Working in other applications.虽然在其他应用程序中使用相同的逻辑。 but in this application its reloading the application not exiting it.但在这个应用程序中,它重新加载了应用程序而不是退出它。

PS: i have also tried it to put in platform.ready() but no luck. PS:我也试过把它放在platform.ready()但没有运气。

With IONIC 4 , there is new method subscribeWithPriority developed to handle race between soft & hard back button.使用IONIC 4 ,开发了新方法subscribeWithPriority来处理软和硬后退按钮之间的竞争。 Try modifying your code like below:尝试修改您的代码,如下所示:

 this.platform.backButton.subscribeWithPriority(1, () => {
        navigator['app'].exitApp();
 });

subscribeWithPriority() stops the propagation of the event after its execution and if we subscribe with high priority and execute our prefered navigation instead of default one then it is going to work as you want. subscribeWithPriority()在事件执行后停止传播,如果我们以高优先级订阅并执行我们的首选导航而不是默认导航,那么它将按您的意愿工作。

More reference docs for details:有关详细信息的更多参考文档:
https://github.com/ionic-team/ionic/commit/6a5aec8b5d76280ced5e8bb8fd9ea6fe75fe6795 https://github.com/ionic-team/ionic/commit/6a5aec8b5d76280ced5e8bb8fd9ea6fe75fe6795
https://medium.com/@aleksandarmitrev/ionic-hardware-back-button-nightmare-9f4af35cbfb0 https://medium.com/@aleksandarmitrev/ionic-hardware-back-button-nightmare-9f4af35cbf​​b0

UPDATES:更新:

  • Try using this new version of exitApp cordova plugin .尝试使用这个新版本的 exitApp cordova plugin I haven't tried myself but looks promising from popularity.我还没有尝试过自己,但从受欢迎程度来看,它看起来很有希望。
  • Also try to empty the page stack from Navcontroller or go to your home screen, seems like that's causing the reload for app with sidemenu's & tab pages... this.navCtrl.pop() / this._navCtrl.navigateBack('HomeScreen') , and then call exitApp .还尝试从 Navcontroller 清空页面堆栈或转到您的主屏幕,这似乎导致重新加载带有this.navCtrl.pop() / this._navCtrl.navigateBack('HomeScreen')和标签页的应用程序... this.navCtrl.pop() / this._navCtrl.navigateBack('HomeScreen') ,然后调用exitApp

NOTE: Tabs & SideMenu as those have its own routing module does create lot of complexity with app navigation.注意: Tabs 和 SideMenu 有自己的路由模块,这确实给应用导航带来了很多复杂性。

Solved:解决了:

As Mention by @rtpHarry template of SideMenu / Tabs have History which leads application to Reload it self on root page.正如@rtpHarry提到的 SideMenu / Tabs 模板具有历史记录,这导致应用程序在根页面上自行重新加载。 i was able to solve this by clearing History.我能够通过清除历史记录来解决这个问题。

ionViewDidEnter(){
  navigator['app'].clearHistory();    
}

on Your Root Page just Clear your history and your Hardware Back Button will close the Application instead of Reloading it.在您的根页面上只需清除您的历史记录,您的硬件后退按钮将关闭应用程序而不是重新加载它。

Do you have a sidemenu in your app?你的应用程序中有侧菜单吗? I'm just curious because this seems to be when I get this problem as well.我只是好奇,因为这似乎也是我遇到这个问题的时候。

If you look in your inspector, you will see that window.history has a length of 1.如果您查看检查器,您将看到window.history的长度为 1。

I don't see it in some of my apps, but the app that I have a side menu setup acts this way - on the homepage if you press back the screen goes white then it reloads the app.我在我的一些应用程序中没有看到它,但是我有侧边菜单设置的应用程序会以这种方式运行 - 在主页上,如果您按回,屏幕会变白,然后它会重新加载应用程序。

Like I say, looking in the inspector shows that there is a history to step back to, which it is trying to do, and whatever that history step is, it just pushes it forward back to the homepage, which made me wonder if it was the sidemenu setting up its own control of the navigation system.就像我说的,查看检查器显示有一个历史可以退回,它正在尝试做,无论历史步骤是什么,它只是将其推回主页,这让我想知道它是否是侧菜单设置自己对导航系统的控制。

I've probably said some poorly worded terminology but as I haven't solved this myself I thought I would just let you know what I had found... hopefully it helps you move forward.我可能说了一些措辞不好的术语,但由于我自己还没有解决这个问题,我想我只会让你知道我的发现......希望它能帮助你前进。

In my scenario, I wasn't even trying to do the exit on back code - I just noticed that the app would appear to "reboot" if I kept pressing back.在我的场景中,我什至没有尝试退出返回代码 - 我只是注意到如果我一直按回,应用程序似乎会“重新启动”。

This explain the solution on Ionic 5 (and 4.6+ too I think).这解释了 Ionic 5(我认为也是 4.6+)上的解决方案。

private backButtonSub: Subscription;

ionViewDidEnter() {
  this.backButtonSub = this.platform.backButton.subscribeWithPriority(
    10000,
    () => {
        // do your stuff
    }
  );
}

ionViewWillLeave() {
  this.backButtonSub.unsubscribe();
}

also keep也保持

IonicModule.forRoot({
  hardwareBackButton: true
}),

to true (default) in your app.module.tsapp.module.ts设置为true (默认)

Sources:资料来源:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM