简体   繁体   English

App Exit无法在Android App(Phonegap和AngularJS)中按返回按钮

[英]App Exit NOT working on pressing the back button in Android App (Phonegap & AngularJS)

I'm new to App dev using AngularJs and PhoneGap, so I may be doing something wrong here (please rectify). 我是使用AngularJs和PhoneGap的App开发人员的新手,因此我在这里可能做错了(请纠正)。 My objective is to make the App exit if anyone presses the BackButton in Android when the /viewInitialDepartments is active (ie when the user is in that view). 我的目标是在/ viewInitialDepartments处于活动状态时(即,当用户处于该视图中时),如果有人在Android中按下BackButton,则使App退出。 Relevant Codes are as follows :- 相关代码如下:

Of my index.html page:- 在我的index.html页面上:

<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width,height=device-height,  initial-scale=1.0, maximum-scale=1.0, user-scalable=0" >
<meta http-equiv="x-ua-compatible" content="ie=10">
<meta name="msapplication-tap-highlight" content="no" >
<link rel="stylesheet" href="css/module-reset.css" ><link rel="stylesheet"  href="css/module-grid.css" ><link rel="stylesheet"  href="css/module-all.css" ><link rel="stylesheet"  href="css/module-transitions.css"><link rel="stylesheet"  href="css/module-custom.css" >  <link rel="stylesheet" href="css/styleMenu.min.css">
<title>ad</title>
<script type="text/javascript" src="cordova.js"></script> 
</head>
<body ng-cloak ng-app="askDadaApp">

<div id="mainBodyWrapper">
<div class="page {{ pageClass }}" ng-view></div>
</div>
<script src="js/angular.min.js"></script>
<script src="js/angular-route.js"></script>
<script src="js/angular-animate.js"></script>
<script src="js/angular-touch.js"></script>
<script src="js/ngprogress.min.js"></script>
<link rel="stylesheet"  href="css/ngProgress.css" >
<script src="js/script-controller.js"></script>
</body>
</html>

Of my Angular JS Contol Page ( script-controller.js ) :- 我的Angular JS Contol页面( script-controller.js ):-

var adApp = angular.module('adApp',['ngRoute', 'ngAnimate','ngTouch','ngProgress']);
adApp.config(function($routeProvider,  $locationProvider) {

$routeProvider
.when('/', {templateUrl: 'views/1-setconnection.html',controller: 'setController' })
.when('/viewInitialDepartments', {templateUrl: 'views/2-viewInitialDepartments.html',controller: 'viewInitialDepartmentsController' })
.when('/offLine', { templateUrl: 'views/1-offLine.html',controller:'offLineController'})
});
// CONTROLLERS
adApp.controller('offLineController', function($scope,  $http, $location, $routeParams ) { $scope.pageClass = 'normal';var notOnlineToast = document.getElementById("notOnlineToast");notOnlineToast.style.display="block";
});
adApp.controller('setController', function($scope, $http, $location) {
var typeList = "CHECKCONNECTION";
$http.get("URL/php/fetchData.php?typeList=" + typeList )
.error(function(response) {$location.path('/offLine');
// if not offline then go to Home View (viewInitialDepartments)
$location.path('/viewInitialDepartments');
});
adApp.controller('viewInitialDepartmentsController', function($scope,  $http, $location, $routeParams) {
var typeList = "CHECKCONNECTION";
$http.get("URL/php/fetchData.php?typeList=" + typeList )
.error(function(response) {
$location.path('/offLine');
});
// REST OF THE CODE
});

My config.xml file has the declaration :-gap:plugin name="org.apache.cordova.dialogs" 我的config.xml文件具有以下声明:-gap:plugin name =“ org.apache.cordova.dialogs”

Now the thing is that I need to know how to make the App exit on pressing the BackButton on Android Phone while the user is in /viewInitialDepartments view. 现在的问题是,当用户处于/ viewInitialDepartments视图中时,我需要知道如何在按Android Phone上的BackButton时退出应用程序。 Please help . 请帮忙 (Everything else is working fine as expected.) (其他所有功能都按预期运行。)

EDIT - I guess this could be achieved using document.addEventListener("deviceready", onDeviceReady, false); 编辑 -我想这可以使用document.addEventListener(“ deviceready”,onDeviceReady,false);来实现。 and document.addEventListener("backbutton") but don't know how to do that in my present code as such that the backButton does what it does for all other views and just make the App close only when in /viewInitialDepartments view. 和document.addEventListener(“ backbutton”)一样,但不知道如何在当前代码中执行该操作,因此backButton会对所有其他视图执行该操作,并且仅在/ viewInitialDepartments视图中关闭应用程序。

Define global variable to store current route(view) name 定义全局变量以存储当前路线(视图)名称

var CURRENT_ROUTE; 

Assign value to CURRENT_ROUTE on route change 更改路线时将值分配给CURRENT_ROUTE

var adApp = angular.module('adApp',['ngRoute', 'ngAnimate','ngTouch','ngProgress']).run([
    '$rootScope',
    function ($rootScope) {
        $rootScope.$on('$routeChangeStart', function (event, next) {
            CURRENT_ROUTE = next;
        });
    }]);

Perform action on back button 对后退按钮执行操作

document.addEventListener("backbutton", function(){
   if(CURRENT_ROUTE === yourviewname){
      navigator.app.exitApp(); // exit app
   }
}, false);

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

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