2
votes

I have an issue with the Android native back command that stops working after adding the codename one toolBar. Should I change the toolBar to an other component ? Below a piece of code :

    homeToolBar = new Toolbar(false);
    int ScreenWidth = Display.getInstance().getDisplayWidth();
    centerCnt.setWidth(ScreenWidth - 2 * ExitCommand.getIcon().getWidth());
    centerCnt.setLayout(new BorderLayout());
    centerCnt.addComponent(BorderLayout.CENTER,btnLogo);
    currentForm.setToolBar(homeToolBar);
    currentForm.addCommand(SideLogoCommand);
    homeToolBar.addCommandToSideMenu(MenuCommand);
    homeToolBar.setTitleComponent(centerCnt);
    homeToolBar.addCommandToRightBar(ExitCommand);
    Command BackCMD = new Command("Back") {
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        System.out.println("Back"); 
                    }
                };
    currentForm.setBackCommand(BackCMD);
1
What is your issue, please share some more info on your code - Chen
I shared a piece af my code ! Actualy I can't use toolbar and android native back command simultaneously , that is my problem . - Adnane17

1 Answers

1
votes

Of Course you can, See a similar question from today here.

As a fix for your code, do this:

homeToolBar = new Toolbar(false);
int ScreenWidth = Display.getInstance().getDisplayWidth();
centerCnt.setWidth(ScreenWidth - 2 * ExitCommand.getIcon().getWidth());
centerCnt.setLayout(new BorderLayout());
centerCnt.addComponent(BorderLayout.CENTER,btnLogo);
currentForm.setToolBar(homeToolBar);
currentForm.addCommand(SideLogoCommand);
homeToolBar.addCommandToSideMenu(MenuCommand);
homeToolBar.setTitleComponent(centerCnt);
homeToolBar.addCommandToRightBar(ExitCommand);
Command BackCMD = new Command("Back") {
    @Override
    public void actionPerformed(ActionEvent evt) {
        System.out.println("Back"); 
    }
};
BackCMD.putClientProperty("uiid", "BackCommand");
currentForm.setBackCommand(BackCMD);
homeToolBar.addCommandToLeftBar(BackCMD);

On a separate note, read about java naming convention here to learn how to write clean and easy to read codes.