androideventstitanium-mobile

Titanium: android options all button events trigger at once


I have created an option menu for my app like this:

var activity = Ti.Android.currentActivity;
        activity.onCreateOptionsMenu = function(e){
            var menu = e.menu;
            var reply = menu.add({});
            var share = menu.add({});
            var facebook = menu.add({});
            
            reply.setIcon('mail_reply.png');
            share.setIcon('mail-replied.png');
            facebook.setIcon('facebook.png');
            
            reply.addEventListener('click', emailReply());
            share.addEventListener('click', emailPublish());
            facebook.addEventListener('click', FBpublish());
        }

The problem is, when I press the options button on my device, all the events trigger at once. After discarding them, the menu comes up but all the events won't fire anymore. Is it because I added empty objects? I don't want them to have a title and I couldn't find a default empty menu item to add.


Solution

  • Found the answer. Apparently, adding the parenthesis to the function name runs the function automatically. I should've written:

    reply.addEventListener('click', emailReply);
    

    instead of:

    reply.addEventListener('click', emailReply());