java-melwuitj2mepolish

Back to previous page in j2me with lwuit using Command


In my project I am going to on one List then clicking on the item of that List going to new Form there is another List on which by clicking going to next Form.

These Forms are not different Forms, they are in a single Form but they are added in a Container and as click on List item it is visualized. The problem is I have a back Button on each Container but I have to code all time when I need to go to back to previous page.

I need a solution such that there will only one back Command on which by clicking it should go to it's current page's previous page. Need only one Command on which by clicking it goes to its previous page.

I know how to code this with COMMAND.BACK in lcdui but need suggestion for LWUIT.


Solution

  • I wouldn't use a Buttonto do this stuff. My suggestion is, use a Command and implement a switch or some if else to set the different back functionality. So:

    int pressed = 0;
    Command c = new Command("Back"){
    
            public void actionPerformed(ActionEvent evt) {
                super.actionPerformed(evt);
                switch(pressed){
                  case 0:
                    //Functionality for first case
                  case 1:
                    //Functionality for second case
                  case 2:
                    //Functionality for third case
                }
    
    
            }
        };
    form.addCommmand(c);
    

    increment the pressed variable every time you press in the Container. and reset it when its necessary.