单击设备后退按钮可关闭应用程序,而不是返回上一页 [英] Clicking the device back button closes the app instead of going back to previous page
问题描述
如果点击任何 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.
因此,当您点击硬件返回按钮时,应该检查您所处的$状态。
So you should check the $state you are in, when you tap the hardware back button.
使用以下代码(放置在模块的运行功能中),您可以覆盖默认行为。例如,你可以像这样禁用app退出:
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);
这篇关于单击设备后退按钮可关闭应用程序,而不是返回上一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!