Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit from app when click button in android phonegap?

I am new to phonegap. I have prepared one sample application. My application has 2 pages, the first page has one button, when clicked the second page will open. It is working fine using the following

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

The second page has one button, when clicked I want to exit from 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 is not working.

like image 513
user1651572 Avatar asked Sep 06 '12 10:09

user1651572


5 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 phonegap js .

like image 83
Chirag Avatar answered Nov 20 '22 19:11

Chirag


There are different ways to close the app, depending on:

if (navigator.app) {
    navigator.app.exitApp();
} else if (navigator.device) {
    navigator.device.exitApp();
} else {
    window.close();
}
like image 26
Ujjwal Kumar Gupta Avatar answered Nov 20 '22 20:11

Ujjwal Kumar Gupta


navigator.app.exitApp();

add this line where you want you exit the application.

like image 11
Anil Singhania Avatar answered Nov 20 '22 21:11

Anil Singhania


@Pradip Kharbuja

In Cordova-2.6.0.js (l. 4032) :

exitApp:function() {
  console.log("Device.exitApp() is deprecated. Use App.exitApp().");
  app.exitApp();
}
like image 3
Shadow Avatar answered Nov 20 '22 21:11

Shadow


if(navigator.app){
        navigator.app.exitApp();
}else if(navigator.device){
        navigator.device.exitApp();
}
like image 2
Stevko Avatar answered Nov 20 '22 20:11

Stevko