user2999294
user2999294

Reputation: 51

Phonegap android app exiting after clicking on back button even though the user is logged-in

I am creating a simple android Login app using PhoneGap and Eclipse and facing problem with android back button.

I want to control the behaviour of back button such as :

  1. If user has been logged in and he/she pressed on back button app should be minimized.
  2. If not logged-in app should get closed after clicking on back button.

I scoured the lots of posts regarding with "Minimizing the app after clicking back button" but didn't got the solution. Any solution?

Upvotes: 1

Views: 2338

Answers (2)

Deep Mehta
Deep Mehta

Reputation: 1280

You can handel phonegap back button events with the help of

document.addEventListener("backbutton", onBackButtonFire, false); 

Handle Application Pause

document.addEventListener("pause", onPause, false);

Handle Application Resume

document.addEventListener("resume", onResume, false);

Define Back Function

function onBackButtonFire()
{
    // Check if user is logged in or not.?
    if(localStorage.Checklogin=="true")
    {
        // minimize app here
    }
    else
    {
        // Exit the app
        navigator.app.exitApp();
    }
}
function onPause()
{
    //handel pause;
}
function onResume()
{
    // handel resume of app
}

store your information about is user logged in in localStorage.Checklogin .

you can find manually pause the application using this link Manually Make application Pause

Upvotes: 3

QuickFix
QuickFix

Reputation: 11721

The default behaviour of the backbutton is navigate back in the history and to close the app if ther's no more history.

In Cordova/Phonegap, you can build your own event handler for the back button.

See doc here : http://cordova.apache.org/docs/en/3.5.0/cordova_events_events.md.html#backbutton

Then in your event handler, you would use the history api to either navigate back if you're not on the first page or perform your own logic to close/reduce the app depending on user logged or not.

It's easy to close the app : navigator.app.exitApp();

But to reduce it, I don't know if there's an easy thing in phonegap. And I honestly don't think it makes any sence as in android there's the home button which basically reduces the app.

What I do in my app is that if user is logged, I display a dialog to ask the user if he wants to disconnect.

Upvotes: 1

Related Questions