Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

android:back event doesn't work

I'm trying to use an android:back event on a heavy-weight window (opened using modal:true) to close the window when the back button is pressed. However this event never fires, is this another bug, I copied the code exactly from Titanium's own example…

win.addEventListener('android:back', function (e) {
    Ti.API.Info("Pressing Back Will Not Close The Activity/Window");
});
— asked December 3rd 2010 by Justin Toth
  • android:back
0 Comments

1 Answer

  • Accepted Answer

    Hi Justin

    You don't need to explicitly configure the back button; it's default function is to close modal views.

    However, if you wish to execute other code and also close the modal view when the user presses the back button, you can do so as follows:

    
    var win = Ti.UI.currentWindow;
    
    win.addEventListener('android:back', function (e) {
      Ti.API.Info("Pressing Back Will Not Close The Activity/Window");
      win.close();  
    });
    

    Note that you may need SDK 1.5.X rather than 1.4.X in order for this to work, which you can obtain from the Continuous Builds page.

    Hope this helps

    — answered December 3rd 2010 by Paul Dowsett
    permalink
    4 Comments
    • The reason I need to handle it explicitly is because not only do I need to close the window but I also need to remove an event listener.

      I have a window with a list of items and when you click on one, it goes to a item details window. Once per minute it updates the list of items and I fire off an event to update the item details window as well. When I fire off this event, it calls it multiple times, once per item details window that I've ever opened. This tells me that either the windows aren't closing when the back button is pressed or even if it is, it's not removing event listeners within that scope.

      Either way, I've tried capturing android:back (see my code above) and it doesn't work. However I haven't tried 1.5 yet, will give that a shot, thanks!

      — commented December 3rd 2010 by Justin Toth
    • The link to http://build.appcelerator.net is broken so I can't try this approach… :(

      — commented December 6th 2010 by Justin Toth
    • The correct link for the CI builds is:
      http://builds.appcelerator.com.s3.amazonaws.com/index.html

      Installed 1.5.0 and android:back event is now working so looks like Ti fixed that bug, awesome!!

      — commented December 6th 2010 by Justin Toth
    • try to use 'androidback' instead of android:back

      — commented July 6th 2013 by vivek gidmare
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.