单击设备后退按钮关闭应用程序而不是返回上一页 [英] Clicking the device back button closes the app instead of going back to previous page

查看:13
本文介绍了单击设备后退按钮关闭应用程序而不是返回上一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果单击任何 ion-item 它会打开所需的页面,但如果我单击设备后退按钮它会关闭应用程序而不是返回到 android 中的上一页:

If click any ion-item it open the desired page but if i click device back button it close the app rather than going back to previous page in android:

这是我的离子侧菜单:

<ion-side-menus enable-menu-with-back-views="false">
  <ion-side-menu-content>
    <ion-nav-bar class="bar-positive">
      <ion-nav-back-button></ion-nav-back-button>
      <ion-nav-buttons side="left">
        <button class="button button-icon icon ion-android-menu" menu-toggle="left">
        </button>
      </ion-nav-buttons>
    </ion-nav-bar>
    <ion-nav-view name="menuContent"></ion-nav-view>
  </ion-side-menu-content>

  <ion-side-menu side="left">
    <ion-header-bar class="bar-positive">
      <h1 class="title"></h1>
    </ion-header-bar>
    <ion-content>
      <ion-list>
        <ion-item menu-close ng-click="login()">
          Login
        </ion-item>
        <ion-item menu-close ui-sref="app.search">
          Search
        </ion-item>
        <ion-item menu-close href="#/app/browse">
          Browse
        </ion-item>
        <ion-item menu-close href="#/app/playlists">
          Playlists
        </ion-item>
      </ion-list>
    </ion-content>
  </ion-side-menu>
</ion-side-menus>

这里是 app.js :

    .state('app', {
    url: '/app',
    abstract: true,
    templateUrl: 'templates/menu.html',
    controller: 'AppCtrl'
  })

  .state('app.search', {
    url: '/search',
    views: {
      'menuContent': {
        templateUrl: 'templates/search/default.html'
      }
    }
  })
  .state('app.search-form', {
    url: '/search-form',
    views: {
      'menuContent': {
        templateUrl: 'templates/search/search-form.html'
      }
    }
  })

我找到的一个解决方案:

$ionicHistory.nextViewOptions({        
  disableBack: true,
  historyRoot: true
});      

所以当你点击一个按钮并进入下一页时,这将禁用后退按钮.

So when you click a button and going to next page, this will will disable back button.

推荐答案

后退按钮的默认行为如下:返回历史记录 - 如果历史记录堆栈为空 -> 退出应用程序.

The default behaviour of the back button is as follows: Go back in history - if the history stack is empty -> exit the app.

因此,当您点击硬件后退按钮时,您应该检查您所处的 $state.

So you should check the $state you are in, when you tap the hardware back button.

使用以下代码(放置在模块的运行函数中)您可以覆盖默认行为.例如,您可以像这样禁用应用退出:

With the following code (placed in the run function of your module) you can overwrite the default behaviour. For example you can disable the app exit like this:

$ionicPlatform.registerBackButtonAction(function (event) {
  if($state.current.name=="app.home"){
    navigator.app.exitApp(); //<-- remove this line to disable the exit
  }
  else {
    navigator.app.backHistory();
  }
}, 100);

请参阅 $ionicPlatform 的文档.

这篇关于单击设备后退按钮关闭应用程序而不是返回上一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆