0
votes

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???

2
Basically you want to say that your application should again go to Dashboard although your current page is dashboard. The behavior what you stated is maintaining session, but using phonegap we are not doing it.Shoaib Chikate
we don't have back button in my HTML Page. But in mobile, we have a back button right??? Now what's the solution????Karthik N
Then that backbutton will directly close your application, and when you open the application again, it will take you to dashboard. You have maintain your credentials in local storage of mobile.Shoaib Chikate
is it jquery mobile page ?Ashisha Nautiyal
We have used Cordova-2.2.js and jquery.mobile-1.0rc1.min.jsKarthik N

2 Answers

1
votes

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);
   }
};
0
votes

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();

        }
           }