Page 1 of 1

Back button Android

Posted: Tue Jan 15, 2013 9:48 pm
by Latchezar Mladenov

Hello, i want to ask you a qustion about the back button on android devices. I make an app with many pages and when I click this button the app return the last page. I want to exit the app when the back button is clicked. Please support.


Back button Android

Posted: Tue Jan 15, 2013 10:06 pm
by Kateryna Grynko

Hello,

We have PhoneGap "Back button" event.

You can add to this event Run JavaScript Action with following JavaScript code:
codenavigator.app.exitApp();/codeYou'll exit the app on "Back" button click.

Each screen has this event. If you want it to work everywhere add this action to all the screns.

Or you can create JavaScript asset:
codedocument.addEventListener("backbutton", function( e )
{
navigator.app.exitApp();
}, false);/codeThis will work globally on each screen.


Back button Android

Posted: Thu Jan 17, 2013 8:11 pm
by Latchezar Mladenov

I created new JavaScript and pasted the code. After that i copied the apk file to my phone and it ran correctly but when i press the back button(I am with Galaxy Nexus Android 4.2) the app returns back screen but it does not exit. What I should do?


Back button Android

Posted: Thu Jan 17, 2013 8:13 pm
by maxkatz

Add an alert to make sure the event is fired.


Back button Android

Posted: Thu Jan 17, 2013 8:38 pm
by Latchezar Mladenov

How to add an alert?


Back button Android

Posted: Thu Jan 17, 2013 8:46 pm
by Kateryna Grynko

You can add following JavaScript:codealert("Alert!");/code


Back button Android

Posted: Thu Jan 17, 2013 9:04 pm
by Latchezar Mladenov

alert("document.addEventListener("backbutton", function( e )
{
navigator.app.exitApp();
}, false);")


Back button Android

Posted: Thu Jan 17, 2013 9:17 pm
by Latchezar Mladenov

Or you can create JavaScript asset:
document.addEventListener("backbutton", function( e )
{
navigator.app.exitApp();
}, false);
This will work globally on each screen.

Where and how to do that? Sorry I am new and I need a little help :)


Back button Android

Posted: Thu Jan 17, 2013 9:41 pm
by Kateryna Grynko

Go to Create New - JavaScript.
Then enter JavaScript file name in appeared window.
JavaScript editor will be opened. Paste the code there.