Latchezar Mladenov
Posts: 0
Joined: Thu Jan 10, 2013 11:19 pm

Back button Android

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.

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Back button Android

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.

Latchezar Mladenov
Posts: 0
Joined: Thu Jan 10, 2013 11:19 pm

Back button Android

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?

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Back button Android

Add an alert to make sure the event is fired.

Latchezar Mladenov
Posts: 0
Joined: Thu Jan 10, 2013 11:19 pm

Back button Android

How to add an alert?

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Back button Android

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

Latchezar Mladenov
Posts: 0
Joined: Thu Jan 10, 2013 11:19 pm

Back button Android

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

Latchezar Mladenov
Posts: 0
Joined: Thu Jan 10, 2013 11:19 pm

Back button Android

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 :)

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Back button Android

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

Return to “Issues”