Android Apps

Handling Back button and close Application – Android app phonegap

July 12, 2012 by Sarath DR in Android Apps, Tips with 1 Comment

This is one of the main difficulties I faced during the early stages of Android phonegap development. Might be lack of experience. This will be a helpful tips for you and also a future reference for me. The following code close the application if it is the starting page and go to history if it is not. You have to give a unique identifier for the starting page might be a div with unique id should be in the homepage.

    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady()
    {
        // Add event listener to the back button
        document.addEventListener("backbutton", backButtonHandler, false);
    }
 
    function backButtonHandler(e )
   {
 
      if( $("#homepageLanding").length > 0 )
      {
         // Code to exit app
         navigator.app.exitApp();
      }
      else
      {
        // Code to navigate to the history action
         navigator.app.backHistory();
      }
}

Tagged ,

Share this to

Related Posts

One Comment

  1. joshAug 24, 2013 at 5:15 amReply

    This worked perfectly!

Leave a reply

Your email address will not be published. Required fields are marked *

*

About
View Sarath D R's profile on LinkedIn

I am a technology consultant and an entrepreneur focused on the following technologies Java,Spring, Android, Angular, Asterisk, Symfony, Cake , Code-Igniter, Jquery , XAMPP, XHTML, AJAX, MySql, Seo, Web Design, Wordpress, iPhone development.

Today’s Tips
  • Generate Codova.jar file from Phonegap project using ant
  • Google map javascript client  to find the geographic co-ordinates  from  Address or post code
  • Update Android main UI from Webview – Phonegap Plugin
  • Jplayer is not working  with HTC One Android 4.3 version browser
  • Regular expression to validate mobile phone numbers starting with + ( plus ) – php
  • Simple pagination script for PHP
  • Useful adb ( Android Debug Bridge ) commands
  • Change the javascript files url to absolute url – Symfony 1.4
  • Set Http authentication header from Client – PHP / CURL

This site uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.This site also uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies.

Close