6

I'm using Phonegap to make a small application but the navigator.app.exitApp()? isn't working at all.

  • This is my first hybrid app.
  • My target platform is Android 5
  • I'm developing on Windows with Cordova CLI.

I call a JavaScript function with this

<input type='button' onclick='exitApp();'/>

JavaScript:

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

Ideas??

Thomas Fifield
  • 137
  • 2
  • 3
  • 9
  • Have you installed notification plugin? – User6006 Feb 02 '16 at 04:37
  • This one?? cordova plugin add https://github.com/katzer/cordova-plugin-local-notifications – Thomas Fifield Feb 02 '16 at 04:49
  • This one :- cordova plugin install org.apache.cordova.dialogs – User6006 Feb 02 '16 at 07:47
  • Please **answer the following questions in your post**. Since this appears to be your on of your first post on this subject. Is this your first hybrid App? What is your target platform and their target versions? Android 4,5,6? iOS 7,8,9? What platform are you developing on? Windows, MacOS? Are you using [CLI, SDK or Build](https://github.com/jessemonroy650/top-phonegap-mistakes/blob/master/new-to-Phonegap.md#001) ? Please do not assume the answer, please read the link. Once you have answer the question *in the post*, respond in the comments so I know you have added information to the post. –  Feb 02 '16 at 08:16
  • Jesse I have answered your question on the post. Thanks – Thomas Fifield Feb 02 '16 at 21:59
  • You need to use the @ sign in front of my name or I cannot see the response. I happened on this by chance. IS THIS YOUR FIRST HYBRID APP? –  Feb 03 '16 at 01:33
  • yes this is my first app, I only need it to work on Android @JesseMonroy650 – Thomas Fifield Feb 03 '16 at 03:40
  • Okay. I have updated your original post. FWIW: *Phonegap Build* is an online build service by Adobe/Phonegap. Your answer will take about 30 minutes. You've asked a very good question, which will do well after they answer is accepted. –  Feb 03 '16 at 05:27

4 Answers4

6

it used to be that calling navigator.app.exitApp() had just a few stumbling blocks, but now both Google and Apple have thrown in major impediments for developers.

  1. Make sure your you wait for the deviceready events before you make the call to exit. You might consider putting up a splash screen, or greying out (disableing) the button or something until deviceready fires and the Cordova library is loaded.
  2. This is the *impediment*. You now need to add a whitelist plugin and for Android add CSP. The plugin is required for CSP. You can get around this by moving all Javascript (including any on*=) and <style> (and style=) into a separate file. EXCEPTION for CSP – using any online resources.

On #1,

Add this to your javascript:

// Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    // alert("deviceready");
    document.getElementById('exitApp').addEventListener('click', function() {
        navigator.app.exitApp();
    });
}

Add this to your index.html:

<button id="exitApp">Exit</button>

On #2, the quick answer is:

Add this to your config.xml

<plugin name="cordova-plugin-whitelist" source="npm" spec="1.1.0" />
<allow-navigation href="*" />
<allow-intent href="*" />
<access origin="*" /> <!-- Required for iOS9 -->

NOTE YOUR APP IS NOW INSECURE. IT IS UP TO YOU TO SECURE YOUR APP.
Add the following to your index.html

<meta http-equiv="Content-Security-Policy" 
         content="default-src *; 
                  style-src * 'self' 'unsafe-inline' 'unsafe-eval'; 
                  script-src * 'self' 'unsafe-inline' 'unsafe-eval';">

NOTE YOUR APP IS NOW INSECURE. IT IS UP TO YOU TO SECURE YOUR APP.
This whitelist worksheet should help when you are ready to be more secure.
HOW TO: apply the Cordova/Phonegap the whitelist system

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
2

Use the following:

function backKeyDown() {
    navigator.notification.confirm("Are you sure you want to exit?", onConfirm, "Please Confirm", "Yes,No"); 
}
function onConfirm(button) {
    if(button==2){//If User selected No, then we just do nothing
        return;
    }else{
        navigator.app.exitApp();// Otherwise we quit the app.
    }
}

You have to install the following plugin:

cordova plugin install org.apache.cordova.dialogs
Pang
  • 9,564
  • 146
  • 81
  • 122
User6006
  • 597
  • 4
  • 21
  • Are today (year 2020) dialog's window necessary to achieve an answer to the main question? Thanks in advance. – Bernard Jun 09 '20 at 12:39
1

you can also just add a listener in your device ready callback

onDeviceReady: function () {

    document.addEventListener('backbutton', function(e){
        e.preventDefault();
        //TODO: throw up your dialog here!
    }, true);

    //other stuff here
}
weagle08
  • 1,763
  • 1
  • 18
  • 27
  • Didn't work, I tried to use the simplest idea and nope @weagle08 – Thomas Fifield Feb 03 '16 at 04:31
  • odd because that same snippet is in production code with a navigator.app.exitApp(); it it works as expected. Do you have a very simple project to reproduce the problem? – weagle08 Feb 03 '16 at 04:43
  • I made a new project with only that and worked... But in mine wont work.... Weird @weagle08 – Thomas Fifield Feb 03 '16 at 05:17
  • are you catching any exceptions? i'm wondering if your object prototype hasn't somehow gotten whacked or maybe you have a listener somewhere catching it and preventing app exit somehow? – weagle08 Feb 03 '16 at 05:48
0

Just use wherever you need in this line (Ionic 3 exactly working)

navigator.app.exitApp();

thats all. enjoy your coding...

Community
  • 1
  • 1
Mahendren Mahisha
  • 1,072
  • 11
  • 9