Titanium Community Questions & Answer Archive

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

Gestures addEventListener does not get added a second time.

I have the following code:

win.addEventListener('focus', function(e){
    Titanium.Gesture.addEventListener('orientationchange', dosomething);

win.addEventListener('blur', function(e){
    Titanium.Gesture.removeEventListener('orientationchange', dosomething);

Everything works the first time I view the window, but if the window gets closed and re-opened the Titanium.Gestures.addEventListener event does not get added a second time, and dosomething is never called (a second time).

FYI. The focus event always gets fired.

Is there a way to have the orientation event only fire when you are on a certain window?

— asked October 12th 2010 by Roger Chapman
  • events
  • gestures
  • iphone
  • orientation

2 Answers

  • Accepted Answer

    Thanks Roger, this works for me too! Great!

    — answered October 27th 2010 by Stefan Schleich
  • Looks like this happens in the KitchenSink App too!

    I found a workaround though:

    I moved the Gesture event to app.js where it only gets called once:

    Ti.Gesture.addEventListener('orientationchange',function(e) {

    Then in my window view I have:

    win.addEventListener('focus', function(e){
        Titanium.App.addEventListener('doSomething', dosomething);
    win.addEventListener('blur', function(e){
        Titanium.App.removeEventListener('doSomething', dosomething);
    Hope this helps someone out there!?
    — answered October 12th 2010 by Roger Chapman
    1 Comment
    • Hi Roger,

      GREAT SOLUTION! I was running into the same problem using the shake function. Your code has changed my perspective on how to code my app for future enhancements! Putting global functions and APIs in the app.js might optimize a lot for me. Thanks

      — commented October 27th 2010 by Marijke Beekman
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.