Karthik N
Karthik N

Reputation: 535

PhoneGap Back Button exit the Application

Currently i am working on Mobile Apps using PhoneGap (Cordova 2.2), JQuery and Javascript. Landing Page is Login Page. So once i entered into Dashboard Page using login credentials, when i click the BACK BUTTON its returns to the login page not stay on dashboard page. What can i do??? Suggestions welcome.

I've tried,

SCRIPT

<head>
<script type="text/javascript" charset="utf-8" src="cordova-2.2.0.js"></script>
<script type="text/javascript" charset="utf-8">

// Call onDeviceReady when Cordova is loaded.
//
// At this point, the document has loaded but cordova-2.2.0.js has not.
// When Cordova is loaded and talking with the native device,
// it will call the event `deviceready`.
//
function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}

// Cordova is loaded and it is now safe to call Cordova methods
//
function onDeviceReady() {
    // Register the event listener
    document.addEventListener("backbutton", onBackKeyDown, false);
}

// Handle the back button
//
function onBackKeyDown() {
    alert("Back Button Clicked"); //called the alert..checking
}

</script>

HTML

<body onload="onLoad()">
</body>

My onDeviceReady and onBackKeyDown Function not working / Fired. Am i missing something???

Upvotes: 0

Views: 844

Answers (2)

Ashisha Nautiyal
Ashisha Nautiyal

Reputation: 1397

Try this

    //after successful  login
        localStorage.setItem('login', 1);
// and your back function callback
        function onBackKeyDown(e) {
        e.preventDefault();
        e.stopPropagation();
        var isLogin = localStorage.getItem('login);
        if (isLogin){
        // stay here
        } else {
        history.back();

        }
           }

Upvotes: 0

Riad
Riad

Reputation: 3860

Try with loggin variable to check if login and do something on the call back function.

function onBackKeyDown(evt) {
     evt.preventDefault();
     evt.stopPropagation();
     if(loggedin=='yes'){
         //dont do anything or show popup
     }else{
         history.back();
     }
}

You need to bind the eventLinstener first, and call the app.initialize() in the page onLoad OR $(document).ready() method.

 var app = {
 // Application Constructor
   initialize: function() {
       this.bindEvents();
   },

   // Bind any events that are required on startup. Common events are:
   // 'load', 'deviceready', 'offline', and 'online'.
   bindEvents: function() {

      document.addEventListener('deviceready', this.onDeviceReady, false);

   },

   onDeviceReady: function() {
       document.addEventListener("backbutton", onBackKeyDown, false);
   }
};

Upvotes: 1

Related Questions