1

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?

Marko
  • 20,385
  • 13
  • 48
  • 64
user2999294
  • 51
  • 1
  • 10
  • Save the logged in user using SharedPerferences after the user has logged in; restore the user in onCreate and the auth state in onResume. Basically, once the user has logged in, you want to restore that user as logged in until the user explicitly chooses to log out (regardless of the App being launched from a paused or stopped state). I've had problems immediately relaunching Cordova when config.xml keepRunning is false on Android 4.1; you're best off letting Android exit the app when it needs resources (default Cordova configuration). – mstrthealias Jun 06 '14 at 07:57
  • myninjaname did you even use phonegap? he wont be able to use sharedPreferences.. as for question, you probably cant control back button behavior as good as youd wish. – Marcin Mikołajczyk Jun 06 '14 at 08:19

2 Answers2

3

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

Community
  • 1
  • 1
Deep Mehta
  • 1,250
  • 1
  • 16
  • 29
1

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.

QuickFix
  • 11,661
  • 2
  • 38
  • 50