Titanium Community Questions & Answer Archive

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

Text Field Focus Breaks When Window is Closed and Reopened

I was silently hoping this would get fixed by 1.4 but it didn't. I don't usually bother submitting bug reports anymore because they get no attention but I guess 50k 'active' developers didn't see this one…or something.

This is reproducible in two apps that make use of textField.focus().

Create a window with a text field in it.
Slap a 'focus' or 'open' event listener on the window that forces focus on the testField.
Open this window.

The first time, the text field will have focus and show the keyboard.

Close the window.

Open the window.

The second time, the text field will ignore focus.

The event listener definitely fires. Focus simply gets ignored. I even went so far as to add a button that calls focus() on the text field…tapping the button does nothing.

At this point, it is impossible to programatically give focus to the text field…the only way is to manually tap the text field. A tap on the text field will give it focus and bring up the keyboard…but nothing else will.

— asked July 29th 2010 by Marc Boucher
0 Comments

4 Answers

  • Put a small delay between the focus of the textField and the opening of the window.

    the_win.addEventListener('open', function() {
    
    setTimeout( function(){the_text_field.focus()} ,  50 );
    })
    

    This worked for me on an iPad popover that had a textarea in it.

    — answered July 30th 2010 by Dan Tamas
    permalink
    1 Comment
    • Delay or no delay, it doesn't work…when the window is closed, and this same window is reopened, focus() just doesn't work. It doesn't work with zero delay…it doesn't work with a delay…it doesn't work if you call in 2000 times in a loop, it doesn't work if you call it in a click event on a button.

      — commented July 31st 2010 by Marc Boucher
  • In fact, there's a number of things that simply stopped working between 1.3.2 (the 1.4 RC1 release) and 1.4. How the crap does a 'release candidate' change so much as to break things? It's a release candidate!

    One thing's for sure, the Appcelerator guys keep me on my toes, constantly rewriting code to circumvent new SDK bugs. Unfortunately, this sometimes involves less than adequate solutions which forces me to rewrite when (if) the bug is fixed in later versions.

    — answered August 1st 2010 by Marc Boucher
    permalink
    0 Comments
  • Maybe the 50k active developers didn't see this one, because it's not happening to them? Could be easier to troubleshoot if you posted a piece of code that reproduces the bug :)

    How are you creating the new window, is it running in the same context or in a separate .js file? Do you use a tab group, navigation group?

    I created a small sample to try and reproduce the bug, but the text field focuses reliably every time, no matter how many times I close and reopen the window.

    I'm using SDK 4.0.1, tested on Titanium 1.4.0 and 1.4.1 continuous build.

    Code: app.js, newWindow.js

    — answered August 2nd 2010 by Goran Skledar
    permalink
    5 Comments
    • sigh

      var window = Titanium.UI.currentWindow;
      var button = Titanium.UI.createButton({top:0,left:0,height:50,width:150,title:'Open'});
      button.addEventListener('click',function() {
      window2.open();
      });
      window.add(button);

      var window2 = Titanium.UI.createWindow();
      var button2 = Titanium.UI.createButton({top:0,left:0,height:50,width:150,title:'Close'});
      button2.addEventListener('click',function() {
      window2.close();
      });
      window2.add(button2);
      var textField = Titanium.UI.createTextField({top:50,left:0,height:50,width:150});
      window2.addEventListener('focus',function() {
      textField.focus();
      });
      window2.add(textField);

      It doesn't get any simpler than this code…this code will work on 1.3.2 and early and not in 1.4 or later (1.4.1 builds).

      focus() works the first time the window is opened and is broken on any other subsequent open. Obviously closing the window is doing something weird.

      This bug/problem happens when run in 'the same context' (as my snippet shows)…but also happens when running in separate .js files (as my apps are coded). It happens if you put it on a timer.

      It happens any time focus is programatically called.

      I call bullcrap on anyone that says it works…even on a timer. Anyone copy and paste this simple code and witness 1.4 and 1.4.1 break under what is basic programming.

      — commented August 2nd 2010 by Marc Boucher
    • BTW, now it's your turn to show your snippet that supposedly works.

      — commented August 2nd 2010 by Marc Boucher
    • I have the same problem, very time update the sdk, it took a lot of time to debug and change the code, and many crash without any error information.

      — commented August 2nd 2010 by lan sheng
    • Goran…let's go…show us something that works that isn't cheating – like creating a new window each time you click on the 'Open' button (of course this will work…focus() works only the first time a window is opened…if you create a new window every time, it's going to work every time.)

      — commented August 2nd 2010 by Marc Boucher
    • Yeah, I had a feeling Goran would run away.

      — commented August 5th 2010 by Marc Boucher
  • This is broke for me as well, did you ever figure out a work around? I am developing for android, 1.2.1 and 1.4.2

    — answered November 15th 2010 by Anthony Webb
    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.