How can i detect home pressed event in phonegap - android

I would like to close the app when the physical home button is pressed - does anyone know how to detect this?
.I try onPause event, but i have Social Shearing option in my application. After clicking dedicated button for shearing, Social Activity starts and my application going to close

After lot of searches, I did't find answer. There is no any event in phonegap for Home button press.

Home button work as a resume in android, you can add listener and implement it:
document.addEventListener("resume", function(){
//handle event here
}, true);

Related

PhoneGap on click backbutton simulate click in home button

I'm looking for change action for backbutton, actually I'm using events to catch backbutton calls and using navigator.app.exitApp(); to close app (in really pause app), but I need backbutton call action like home button.
Why? Well, I using cordova-plugin-background-mode, and this works only when app send to background (if using navigator.app.appExit() app is paused), so I need leave app every in background.
Is possible change action of backbutton to works like home?
I want use in iPhone and Android systems
you can use the plugin
https://github.com/amitsinha559/cordova-plugin-back-as-home
and
https://github.com/tomloprod/cordova-plugin-appminimize
after adding the plugin you can minimize the app when ever you want
like on back button click
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown() {
// Handle the back button
window.plugins.appMinimize.minimize();
}
Whichever suits you. Unfortunately they both are for android. you need to create plugin manually for iPhone.

Back button with Cordova/phonegap

I am creating an application using the framework cordova. I need to intercept the "Back" button of the phone itself in order to perform the various aoperazioni, for example closing a modal dialog. The problem is that now, once you open the modal, I can not close it by pressing the back button. What happens is that you close any application. How could I do to intercept this event which is not part of the app but the phone? I refer to the actual button of a smartphone. Thanks for your help.
You can achieve this with Javascript using Phonegap's Event API
document.addEventListener("backbutton", function (e) {
// your code
} , false);
Have you read the docs?
Here's what you need: http://docs.phonegap.com/en/4.0.0/cordova_events_events.md.html#backbutton

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 :
If user has been logged in and he/she pressed on back button app should be minimized.
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?
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
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.

How to handle android HOME button using libgdx

i`m writing game for android using libgdx.
And have problem that game controls are near android buttons HOME and BACK.
There is easy way to handle BACK button, but i havent found something for HOME button. All i want - when user accidentally presses HOME button - to handle it and popup dialog if user want to leave game but not to minimize it at once.
You can't handle Home button (nor Lock Screen).
Reference: How to catch Home/Lock Screen button
For anyone who might stumble on this, a plausible solution would be to implement the onPause method in your android launcher
#Override
protected void onPause(){
//do whatever you need to on pause
super.onPause();
}

Reset history in html when on home page?

Okay, I'm making app with phonegap, and in the app I have a home button which goes to the home screen of the app, but if I click the back key after I touched the home button it goes to the page I was on before I clicked the home button, is it possible to reset the history when you navigate to the homepage? Thanks :)
You can't clear the history.
You can listen for back button event:
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown() {
// Handle the back button
}
and stop the propagation.
The link below recommends using location.replace(url) instead of replacing the history completely, which I'm not sure is possible anyway. However, given that this is a constrained environment (an app) wiping the history might make sense - again, if possible if you're in an app.
Clear browser history
Alternatively, couldn't you just remove the back key and replace it with a custom one?
inside the event handler of the button which takes you to home:
startActivity(activityForHome); //I guess you already have this line
finish(); //now add this line

Categories

Resources