Titanium Community Questions & Answer Archive

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

Add Listener on win.backButton

Is it possible to add an eventListener on win.backButton?

I'm trying to save some information from a textfield when user clicks the back button.

currently i'm passing the information to the "mother window" when the textfield blurs. on the simulator a blur event is fired even when clicking the back button but not on the iphone device.

any idea?

thank you

— asked May 3rd 2010 by Florian Bergmann
  • backbutton
  • form
  • information
  • iphone
  • passing
  • textfield
  • window
0 Comments

2 Answers

  • Accepted Answer

    You can replace the left nav button with a custom button and then add Event Listener to it. This works in simulator:

    var win = Titanium.UI.currentWindow;
    var b = Titanium.UI.createButton({title:'Back'});
    win.leftNavButton = b;
    b.addEventListener('click', function()
    {
       alert('I was clicked'); // to confirm its being called
       // do the stuff here
       win.close();
    });
    
    — answered May 3rd 2010 by Masood Nasir
    permalink
    0 Comments
  • Thank you, great. I've made now a much more complex solution but it works too and might be handy in some other case:

    on the main window:

    Titanium.App.addEventListener('saveForm', function(e){
      if (e.fid == "domagic" && e.value == "true") {
        doMagic();
      }else{
        form[e.fid] = e.value;
        nomagic();
      }
    }
    
    childWin.addEventListener("close", function(e) {
       if (form.foobar != "") {
         Titanium.App.fireEvent("saveForm",{form: "domagic", value: "true"});
      } else {
         Titanium.App.fireEvent("saveForm",{form: "domagic", value: "false"});
      }
    }
    

    and on the child window

    
    someField.addEventListener('change', function(e){ 
        Titanium.App.fireEvent("saveForm",{form: e.source.fid, value: e.source.value});
    });
    
    — answered May 3rd 2010 by Florian Bergmann
    permalink
    0 Comments
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.