Fire Event execute multiple time.

You must Login before you can answer or comment on any questions.

Hi,I am developing android application with Titanium, android sdk 1.8.0.1.In my application there are three tabs.Every Time when I click on tab my window get refreshed.So my code structure looks like:

////// m.js  /////////////////
var explore = Titanium.UI.createWindow(
{
  //navBarHidden:true,
  backgroundColor:'#f8f8f8'
});explore.open({animated:true});
 
Ti.App.addEventListener('feed_partial_action',function(e)
{
  alert('inside event')
})
var new = Titanium.UI.createButton(
{
 
});explore.add(new);
new.addEventListener('click', function(e)
{
  var explore_new = Titanium.UI.createWindow(
  {
    navBarHidden:true,
    backgroundColor:'#f8f8f8'
  });explore_new.open({animated:true});
 
});
/////// explore_new.js/////////////////
var explore_new = Titanium.UI.currentWindow;
Ti.App.fireEvent('feed_partial_action',{page_type:'new'});
so my problem is that on first load alert inside the event listner in m.js executed once but when I again call m.js it shows alert twice.on third time it shows alert 3 times and so on and after some time it forcefully close the app.I think event listener of window still open after refreshing widow.So is there any way to handle this problem.Thank you

3 Answers

Accepted Answer

hi nilesh,

this can be happened because when m.js window open then feed_partial_action add every time but you did not remove that listener so try this

var test = function(e){
    alert(' << TEST >> ');
}
 
Ti.App.addEventListener('feed_partial_action',test);
and on the close event of the window
explore.addEventListener('close',function(e){
    Ti.App.removeEventListener('feed_partial_action',test);
});

— answered 1 year ago by Mitul Bhalia
answer permalink
14 Comments
  • Hi,Mitul thank you for reply its working for me.I just do modification that instead of calling window close function; I remove event inside event listener.eg:

    Ti.App.addEventListener('feed_partial_action',function(e)
    {
       do task here.......
       Ti.App.removeEventListener('feed_partial_action');
    });
    And its working fine. But one question remain in mind that every time refreshing it's not removing that event not releasing my memory also.

    — commented 1 year ago by nilesh kashid

  • when you remove your eventListener you should pass the function as parameter so that i gave you the code like above..so try that

    var test = function(e){
        alert(' << TEST >> ');
        Ti.App.removeEventListener('feed_partial_action',test);
    }
     
    Ti.App.addEventListener('feed_partial_action',test);

    — commented 1 year ago by Mitul Bhalia

  • Mitul if I do in following way please see the code and suggest me is there any problem.

    /////// explore_new.js/////////////////
    var explore_new = Titanium.UI.currentWindow;
    Ti.App.fireEvent('feed_partial_action',{page_type:'new'});
    ////// in m.js i am doing this /////////////////
     
    Ti.App.addEventListener('feed_partial_action',function(e)
    {
        row.add(e.pagetype)
    // instead of calling Ti.App.addEventListener('feed_partial_action',test) I am doing that task in event listener
     
    //////// after completion of task ////////////////////
         Ti.App.removeEventListener('feed_partial_action');
     
    })
    Is this proper way to handle event listeners or causes other issues because my code working fine.is it causing memory related issues since i am doing task inside the event listeners. Thank you once again..........

    — commented 1 year ago by nilesh kashid

  • Show 11 more comments

Hi Nilesh,

When you call m.js you are actually creating one more window because of every time you have created window in m.js.Try to use like this that may help you.

var explore = Titanium.UI.currentWindow;

— answered 1 year ago by Nitin Chavda
answer permalink
1 Comment
  • hi Nitin I tried your solution.I put my createWindow code in another file from where it is initial load and put currentWindow in m.js.But it's not working for me. I think I have to Close that fired event every time or It may memory releasing issue.

    — commented 1 year ago by nilesh kashid

This worked fine for me. Try this too. :)

var test = function(e) {

alert(' TEST ');

Ti.App.removeEventListener('feed_partial_action',  test);

}

Ti.App.addEventListener('feed_partial_action', test);

Your Answer

Think you can help? Login to answer this question!