Titanium Community Questions & Answer Archive

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

How to call function on load of window

I'm using a tabgroup which has a tab that is associated to a window (external js file.) At the bottom of the external js file I call a function "loadData();" which retrieves and displays the data for that window.

When I first click on the tab it loads the data just fine. However, if I go back to the home window then click on the tab again, it doesn't call loadData() this time.

How do I get it to call loadData() every time that tab is clicked? I tried this but it did nothing:

Ti.UI.currentWindow.addEventListener('focus', loadData);

— asked November 1st 2010 by Justin Toth
  • window

5 Answers

  • hi justin

    Try this

    Ti.UI.currentWindow.addEventListener('open', loadData);

    if you cant set it right with the above code then take a look at the scope cause variables wont be available across windows…

    — answered November 1st 2010 by Satta Ravi
  • Thanks for the reply but it didn't work, loadData never gets called, not even on the first tab click. Did I mention this is Android? In case that makes a difference…

    — answered November 1st 2010 by Justin Toth
    1 Comment
    • android or iPhone doesn matter in this case

      — commented November 2nd 2010 by Satta Ravi
  • Hey justin,,

    i think it might be due the scope of the variables… can you post us some sample code so that i can check if it was due to the scope…

    i really ran into this kind of problem in my project last week….

    — answered November 2nd 2010 by Satta Ravi
  • Sure thing. Here is my app.js:

    // this sets the background color of the master UIView (when there are no windows/tab groups on it)
    //create windows.
    var winHome = Ti.UI.createWindow({  
        url: "home.js"
    var winScoreboard = Ti.UI.createWindow({
        title: 'Scoreboard',
        backgroundColor: '#fff',
        url: "scoreboard.js"
    //create tabs.
    var tabGroup = Ti.UI.createTabGroup();
    var tab1 = Ti.UI.createTab({
        icon: 'KS_nav_views.png',
        title: 'Home',
        window: winHome
    var tab2 = Ti.UI.createTab({
        icon: 'KS_nav_ui.png',
        title: 'Scoreboard',
        window: winScoreboard

    And here is the relevant part of my scoreboard.js:

    //global vars.
    var console = { log: Ti.API.info };
    var win = Ti.UI.currentWindow;
    var progress = Ti.UI.createActivityIndicator({ height: 50, width: 10 });
    //load the sports then the games.
    win.addEventListener('open', loadSports);
    function loadSports() {
        //show progress.
        progress.message = "Loading sports...";
        //make ajax call to get sports.
        var loader = Ti.Network.createHTTPClient();
        loader.open("GET", url + "/GetSports");
        loader.onload = displaySports;
    — answered November 2nd 2010 by Justin Toth
    • hey justin, just add a Ti.APi.info inside loadData() definition to make sure it is called(because functions always get called–learnt it last week)and also add Ti.API.info to see if the variables retains values.(this is where i went wrong,mine shows "variables undefined" as i navigate to second window and come back to the main window,people say it coz of scope of windows)

      — commented November 2nd 2010 by Satta Ravi
    • I'm 100% sure it doesn't get called, I am logging to console at the beginning of loadSports() and it doesn't get called ever.

      — commented November 3rd 2010 by Justin Toth
  • I realized that there were a few bugs (really more than a few) in Appcelerator that were causing the issue and was able to work around it:

    1. Window events don't get fired when switching tabs on a tab group. Many others have reported this bug.
    2. When clicking on a tab for a second time, it's supposed to show the tab's content as it was from the first click. However there are screen repainting bugs with Android related to this. Two cases I saw where the screen wouldn't repaint the content:
      a. When setting clearOnEdit on a TextField, the second time you go to the tab it will show the TextField as blank.
      b. When having content in a view within a table row, all of the content within the view will disappear and all you can see are the empty rows.
      How do I know that it's a repaint issue? All you need to do is scroll down and you'll see the rows populated, scroll back up and the rows that were empty now are populated.

    I really hope that they put some time into making things work for Android, I've run into more bugs than I can count in just a week of dev.

    — answered November 4th 2010 by Justin Toth
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.