Exit the application when a button is pressed in the phone of an Android phone

I am new to the phone. I prepared one sample application. My application has 2 pages, the first page has one button, when clicked, the second page opens. It works fine using the following

function callAnothePage() { window.location = "test.html"; } 

There is one button on the second page, when I click on it I want to exit the application. I used the following

  function exitFromApp() { navigator.app.exitApp(); } 

test.html code,

 <!DOCTYPE html> <html> <head> <script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script> <script type="text/javascript" charset="utf-8"> function onLoad() { console.log("device reday !!!!") document.addEventListener("deviceready", onDeviceReady, true); } function exitFromApp() { console.log("in button"); navigator.app.exitApp(); } </script> </head> <body onload="onLoad();"> <h4><center>Login Page</center></h4> <input type="submit" onclick="exitFromApp()" value="exit"/> </body> </html> 

But it does not work.

+56
android cordova phonegap-plugins
Sep 06 '12 at 10:00
source share
7 answers

Try this code.

 <!DOCTYPE HTML> <html> <head> <title>PhoneGap</title> <script type="text/javascript" charset="utf-8" src="cordova-1.5.0.js"></script> <script type="text/javascript" charset="utf-8"> function onLoad() { document.addEventListener("deviceready", onDeviceReady, true); } function exitFromApp() { navigator.app.exitApp(); } </script> </head> <body onload="onLoad();"> <button name="buttonClick" onclick="exitFromApp()">Click Me!</button> </body> </html> 

Replace src = "cordova-1.5.0.js" with your js handset.

+64
Sep 06 '12 at 10:26
source share

There are various ways to close the application, depending on:

 if (navigator.app) { navigator.app.exitApp(); } else if (navigator.device) { navigator.device.exitApp(); } else { window.close(); } 
+16
Apr 19 '16 at 5:14
source share
 navigator.app.exitApp(); 

add this line where you want to exit the application.

+11
Oct 14 '14 at 23:42
source share

@ Pradeep Harbudge

In Cordoba-2.6.0.js (1.4032):

 exitApp:function() { console.log("Device.exitApp() is deprecated. Use App.exitApp()."); app.exitApp(); } 
+3
Apr 24 '13 at 8:12
source share
 if(navigator.app){ navigator.app.exitApp(); }else if(navigator.device){ navigator.device.exitApp(); } 
+2
Mar 16 '16 at 22:09
source share

Sorry, I can not answer in the comments. just FYI, these codes

 if (navigator.app) { navigator.app.exitApp(); } else if (navigator.device) { navigator.device.exitApp(); } else { window.close(); } 

i confirmation does not work. I am using phonegap 6.0.5 and cordova 6.2.0

+1
Aug 04 '16 at 9:01
source share

I used a combination of the above because my application works both in the browser and on the device. The problem with the browser is that it will not allow you to close the window from the script if your application was not opened by a script (for example, browsersync).

  if (typeof cordova !== 'undefined') { if (navigator.app) { navigator.app.exitApp(); } else if (navigator.device) { navigator.device.exitApp(); } } else { window.close(); $timeout(function () { self.showCloseMessage = true; //since the browser can't be closed (otherwise this line would never run), ask the user to close the window }); } 
0
Sep 05 '16 at 16:53 on
source share



All Articles