Search code examples
titanium-mobile

Titanium up caret and menu item on actioinbar not working


I have the following code for a window. I'm trying to implement the up caret so a user can go to the previous window by tapping the title. The up caret appears, but when i tap it, nothing happens, it only glows. The menu item i added doesn't show either. Is there something i'm not doing right?

function ConfirmOrder(_args) {
    var actionBar;
    var self = Ti.UI.createWindow({
        title           : "Confirm Order",
        backgroundColor :'transparent',
        orientation     : 'vertical'
    });

    self.addEventListener("open", function() {
        actionBar = self.activity.actionBar;
        if (actionBar) {
            actionBar.title = "Confirm Order";
            actionBar.displayHomeAsUp = true; // shows up caret
            actionBar.onHomeIconItemSelected = function() {
                self.close();
            };
        } else {
            alert('actionbar not accessible');
        }

        self.activity.onCreateOptionsMenu = function(e) {
            var menu = e.menu;
            var menuItem1 = menu.add({
                title        : L('settings'),
                icon         : "images/Setting.png",
                showAsAction : Ti.Android.SHOW_AS_ACTION_IF_ROOM
            });

            menuItem1.addEventListener("click", function(e) {
                alert("Settings Options Menu");
            });

        };
    });


    var imgView = Ti.UI.createImageView({
        top    : '3%',
        image  : Titanium.App.Properties.getString('image'),
        height : '30%',
        width  : '60%'
    });

    var bottom = Titanium.UI.createView({
        top             : '37%',
        height          : '55%',
        orientation     : 'vertical',
        touchEnabled    : false,
        backgroundImage : '/images/bg2.jpg',
    });

    var buttonsView = Titanium.UI.createView({
        bottom          : '0%',
        height          : '10%',
        orientation     : 'horizontal',
        touchEnabled    : false,
    });


    var confirmButton = Ti.UI.createButton({
        right  : '5%',
        bottom : '10%',
        width  : '40%',
        height : '70%',
        color  : 'white',
        font   : {fontSize:20, fontWeight:'bold', fontColor:'white', fontFamily:'Helvetica Neue'},
        title  : 'Confirm'
    });
    confirmButton.addEventListener('click', function(evt) {
        // blah blah blah
    });

    var cancelButton = Ti.UI.createButton({
        left   : '5%',
        bottom : '10%',
        width  : '40%',
        height : '70%',
        color  : 'white',
        font   : {fontSize:20, fontWeight:'bold', fontColor:'white', fontFamily:'Helvetica Neue'},
        title  : 'Cancel'
    });
    cancelButton.addEventListener('click', function(evt) {
        // blah blah blah
    });


    // add buttons to buttons container
    buttonsView.add(confirmButton);
    buttonsView.add(cancelButton);

    // add parent containers to windows
    self.add(imgView);
    self.add(bottom);
    self.add(buttonsView);
    return self;
};

module.exports = ConfirmOrder;

Thanks all


Solution

  • I can't quite put my finger on it because it looks very similar to how i do it. However the only thing that i have different is that I have another check to ensure that you can get the activity, could it fall into this trap?

    if (! self.getActivity()) {
        Ti.API.error("Can't access action bar on a lightweight window.");
    } else {
        actionBar = self.activity.actionBar;
            if (actionBar) {
                actionBar.title = "Confirm Order";
                actionBar.displayHomeAsUp = true; // shows up caret
                actionBar.onHomeIconItemSelected = function() {
                    self.close();
                };
            } else {
                 alert('actionbar not accessible');
            }
    }
    

    If the other options aren't working either I would remove them to see if they are somehow causing the up caret to error.

    Hope that helps