简体   繁体   English

如何使硬件后退按钮成为退出应用程序

[英]how do make hardware back button become exit app

I found this code but its for javascript: 我找到了这段代码,但它适用于javascript:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady(){
    document.addEventListener("backbutton", function(e){
       if($.mobile.activePage.is('#homepage')){
           e.preventDefault();
           navigator.app.exitApp();
       }
       else {
           navigator.app.backHistory()
       }
    }, false);
}

this case is like my case, that I want to make the back button become the exit way of my app when in certain page (in this case is /home) but this code is written in javascript, I want to make this in angularjs, how do I write it? 这种情况就像我的情况一样,我想使后退按钮成为某个页面(在这种情况下是/ home)时我的应用程序的退出方式,但是这段代码是用javascript编写的,我想在angularjs中使用它,我该怎么写? thanks 谢谢

Angular is JavaScript. Angular是JavaScript。 This code requires no modification to work with angular, you just need to add it to your app. 此代码无需修改即可使用angular,只需将其添加到您的应用中即可。

This is a sample code for illustration that uses Angular ui-router framework which is a standard now: 这是使用Angular ui-router框架进行说明的示例代码,该框架现已成为标准:

Read the code comments for better understanding. 阅读代码注释以更好地理解。 Please modify the below to your needs. 请根据您的需要修改以下内容。 You have to call the exit code only when the cordova library is loaded. 仅当加载了cordova库时,才需要调用退出代码。 So call the angular initialization code inside deviceready event callback. 因此,请在deviceready事件回调内调用角度初始化代码。

Sample HTML code: 示例HTML代码:

<!DOCTYPE html>
<html>
    <head>      
    <script src="angular.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="angular-ui-router.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body ng-app="myApp">
        <div ui-view></div>
        <script src="app.js" type="text/javascript" charset="utf-8"></script>
    </body>
</html>

Sample JS code: Call after the cordova is loaded inside deviceready JS示例代码:将cordova加载到deviceready中后调用

angular.module('myApp', ['ui.router'])
.config(['$stateProvider', '$urlRouterProvider' , function ($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise("/page1")
    $stateProvider
    .state('page1', {             
          url: "/page1",
          template: '<div><div>{{topmsg}}</div><a  ui-sref="page2">Middle</a></div>',
          controller: function ($scope) {
            $scope.topmsg = "loaded top!";
            console.log("Page 1");
          }
    })

    .state('page2', {             
          url: "/page2",
          template: '<div>{{topmsg}}</div>',
          controller: function ($scope) {
            $scope.topmsg = "Pages 2";
            console.log("Page 2");
          }
    });        
}])

.run(function($rootScope, $state){
    $rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams, options){
        console.log("state - "+ toState.name);
            //debugger;
        if(toState.name == "page2"){
            //Add Your closing code here
            //navigator.app.exitApp();
        }       
    }); 
});

Explaination: 说明:

I have created two pages. 我已经创建了两个页面。 Just for illustration while navigating to the second page i am closing the app by calling navigator.app.exitApp() . 出于说明目的,导航到第二页时,我通过调用navigator.app.exitApp()关闭该应用程序。

Refer this documentation for parameters that are available on stage change event. 有关阶段更改事件可用的参数,请参考此文档

Note: iOS Appstore rejects your app if you quit the app from code. 注意:如果您从代码中退出应用,iOS Appstore会拒绝您的应用。

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

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