0

We show a SPLASH screen using form which is followed with MENU form that holds many buttons like about-us, login, feedback etc. Now I move to LOGIN form that holds menu and back commands. My problem is when the back command is clicked in this form it goes to SPLASH screen.

The expected behavior is the back command in LOGIN form must go back to MENU form and not SPLASH form.

How to do this?

Splash.java

public class Splash extends MIDlet implements ActionListener {
    Image img_Splash;
    Form frm_Main;
    String str_URL;
    int Width, Height;
    Label lbl_Splash;
    Command cmd_Cancel;

    public void startApp() {
        Display.init(this);

        frm_Main = new Form();
        try {
            //Resources theme = Resources.open("/44444.res");
            img_Splash = Image.createImage("/res/splash.png");
            Resources theme = Resources.open("/666666.res");
            UIManager.getInstance().setThemeProps(
                    theme.getTheme(theme.getThemeResourceNames()[0]));
        } catch (IOException io) {
            io.printStackTrace();
            Dialog.show("Theme Exception", io.getMessage(), "Ok", null);
        }


        lbl_Splash = new Label(img_Splash);
        lbl_Splash.getStyle().setBgColor(16777215);

        frm_Main.addComponent(lbl_Splash);
        frm_Main.setScrollable(true);

        cmd_Cancel = new Command("Cancel");
        frm_Main.addCommand(cmd_Cancel);
        frm_Main.addCommandListener(this);

        frm_Main.show();

        // th
        Thread splashThread = new Thread() {

            public void run() {
                try {
                    sleep(5000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();

                } finally {
                    MainMenu mainmenu = new MainMenu(frm_Main);
                    mainmenu.show();
                }
            }
        };
        splashThread.start();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        //notifyDestroyed();
    }

    public void actionPerformed(ActionEvent ae) {
        Command con = ae.getCommand();
        String str_CmdName = con.getCommandName();

        if (str_CmdName.equals("Cancel")) {
            notifyDestroyed();
        }
    }
}

Mainmenu.java

class MainMenu extends Form implements ActionListener {

    Form frm_Main;
    //MainMidlet main;
    public Button btn_main_Login, btn_main_NewUser, btn_main_Help;
    public Label lbl_main_Login, lbl_main_NewUser, lbl_main_Help;

    public Image img_main_Login, img_main_NewUser, img_main_Help;
    public Command cmd_Exit, cmd_Select;

    public MainMenu(Form form) {
        this.frm_Main = form;
        try {
            img_main_Login = Image.createImage("/res/btn_main_login.png");
            img_main_NewUser = Image.createImage("/res/btn_main_newuser.png");
            img_main_Help = Image.createImage("/res/btn_main_help.png");
        } catch (IOException io) {
            io.printStackTrace();
            //Dialog.show("Image not Found!", io.getMessage(), "Ok", null);
            System.out.println("Image not found" + io.getMessage());
        }


        //---------   Whole form define as BorderLayout with three Container.
        BorderLayout bor_MainMenuLayout = new BorderLayout();
        setLayout(bor_MainMenuLayout);
        setScrollable(false);

        //----------- Define Container for Header On North
        Container con_Header = new Container();
        lbl_Header = new Label(img_Header);
        con_Header.addComponent(lbl_Header);


        //----------- Define Container as GridLayout and place
        //----------- on BorderLayout Center position
        //---------     Define Container(CENTER) for search details
        //---------     with BoxLayout
        BoxLayout bx_CenterLayout = new BoxLayout(BoxLayout.Y_AXIS);
        Container con_Center = new Container(bx_CenterLayout);
        con_Center.setScrollableY(true);
        con_Center.getStyle().setPadding(0, 10, 3, 3);

        //---------     FlowLayout for Header Details
        FlowLayout flw_MenuHdr = new FlowLayout();
        Container con_HdrDetails = new Container(flw_MenuHdr);
        lbl_MenuHdr = new Label("Menu");
        lbl_MenuHdr.getStyle().setBgTransparency(0);
        lbl_MenuHdr.getStyle().setPadding(0, 5, 10, 0);

        con_HdrDetails.addComponent(lbl_MenuHdr);

        con_Center.addComponent(con_HdrDetails);

        //----------        GridLayout define 1 rows and 3 coloumn
        //----------        First row
        GridLayout grd_Row1Layout = new GridLayout(1, 3);
        Container con_Row1Layout = new Container(grd_Row1Layout);

        btn_main_Login = new Button(img_main_Login);
        btn_main_Help = new Button(img_main_Help;
        btn_main_NewUser = new Button(img_main_NewUser);

        con_Row1Layout.addComponent(btn_main_Login);
        con_Row1Layout.addComponent(btn_main_Help);
        con_Row1Layout.addComponent(btn_main_NewUser);

        con_Center.addComponent(con_Row1Layout);



        //-----------   Add Command on softkey 
        cmd_Exit = new Command("Exit");
        cmd_Select = new Command("Select");

        addCommand(cmd_Select);
        addCommand(cmd_Exit);
        addCommandListener(this);

        addComponent(BorderLayout.NORTH, con_Header);
        addComponent(BorderLayout.CENTER, con_Center);

        //this.getStyle().setBgColor(12105912);
        frm_Main.getStyle().setBgColor(12105912);

    }

    public void actionPerformed(ActionEvent ae) {
        Command cmd = ae.getCommand();
        String str_CmdName = cmd.getCommandName();

        //-----------       When Click on Exit Button
        if (str_CmdName.equals("Exit")) {
            //System.exit(0);
            //frm_Main.notifyAll();
        }

        //-----------       When Click on Select Button
        if (str_CmdName.equals("Select")) {
            //-----------       When Click on Login
            if (btn_main_Login.hasFocus()) {
                String str_MemberId = GlobalClass.getInstance().getMemberId();

                if (!(str_MemberId == "" || str_MemberId == null)) {
                    SearchDiamond searchdiamond = new SearchDiamond(frm_Main);
                    searchdiamond.setTransitionInAnimator(
                            Transition3D.createRotation(800, true));
                    searchdiamond.show();
                } else {
                    Login login = new Login(frm_Main);
                    login.setTransitionInAnimator(
                            Transition3D.createRotation(800, true));
                    login.show();
                }

            }

            //-----------       When Click on New User
            if (btn_main_NewUser.hasFocus()) {
                //Dialog.show("New User", "NewUser", "Ok", null);
                NewUser newuser = new NewUser(frm_Main);
                //newuser.setTransitionInAnimator(
                        CommonTransitions.createFade(1500));
                newuser.setTransitionInAnimator(
                        Transition3D.createRotation(800, true));
                newuser.show();

            }

            //-----------       When Click on Help
            if (btn_main_Help.hasFocus()) {
                Help help = new Help(frm_Main);
                help.setTransitionInAnimator(
                        Transition3D.createRotation(800, true));
                help.show();
            }

            //-----------       When Click on Exit
            if (btn_main_Exit.hasFocus()) {
                System.exit(0);
            }

        }
    }
}

Login.java

class Login extends Form implements ActionListener {

    Form frm_Main;
    Label lbl_Header, lbl_Footer, lbl_SepLine, lbl_HdrAlreadyMember, lbl_UserID, lbl_PWD,
            lbl_Blank2, lbl_Blank3, lbl_HdrNewMember, lbl_Info;
    TextField txtf_UserID, txtf_PWD;
    Button btn_Login;
    Command cmd_Back, cmd_AboutUs, cmd_Privacy;

    public Login(Form form) {
        this.frm_Main = form;

        //---------   Whole form define as BorderLayout with three Container.
        BorderLayout bor_LoginLayout = new BorderLayout();
        setLayout(bor_LoginLayout);
        setScrollable(false);

        //---------   Define Container(NORTH) for Header.
        Container con_Header = new Container();
        lbl_Header = new Label();
        con_Header.addComponent(lbl_Header);


        //---------     Define Container(CENTER) for search details 
        //---------     with BoxLayout
        BoxLayout bx_CenterLayout = new BoxLayout(BoxLayout.Y_AXIS);
        Container con_Center = new Container(bx_CenterLayout);
        con_Center.setScrollableY(true);
        con_Center.getStyle().setMargin(5, 5, 0, 0);

        //---------     FlowLayout for Header Details
        FlowLayout flw_HdrLayout = new FlowLayout();
        Container con_HdrDetails = new Container(flw_HdrLayout);

        lbl_HdrAlreadyMember = new Label("Already a Member?");
        lbl_HdrAlreadyMember.setAlignment(Component.LEFT);
        con_HdrDetails.addComponent(lbl_HdrAlreadyMember);

        con_Center.addComponent(con_HdrDetails);

        //---------     BoxLayout for Login details
        BoxLayout bx_Login1Layout = new BoxLayout(BoxLayout.Y_AXIS);
        Container con_Login1Details = new Container(bx_Login1Layout);
        con_Login1Details.setScrollableY(true);


        lbl_UserID = new Label("User Name:");


            txtf_UserID = new TextField("");
        txtf_UserID.setFocusable(true);
        txtf_UserID.setFocus(true);
        txtf_UserID.setConstraint(TextField.ANY);
        txtf_UserID.setInputMode("abc");

        lbl_PWD = new Label("Password:");

        txtf_PWD = new TextField("");
        txtf_PWD.setConstraint(TextField.PASSWORD);


        con_Login1Details.addComponent(lbl_UserID);
        con_Login1Details.addComponent(txtf_UserID);
        con_Login1Details.addComponent(lbl_PWD);
        con_Login1Details.addComponent(txtf_PWD);

        //---------     BoxLayout for Login Button
        FlowLayout flw_LoginBtnLayout = new FlowLayout(CENTER);
        Container con_LoginBtnDetails = new Container(
                flw_LoginBtnLayout);
        con_LoginBtnDetails.setScrollableY(true);

        btn_Login = new Button("Login");
        btn_Login.setPreferredSize(new Dimension(80, 25));
        btn_Login.setAlignment(Component.CENTER);

        con_LoginBtnDetails.addComponent(btn_Login);

        con_Login1Details.addComponent(con_LoginBtnDetails);

        con_Center.addComponent(con_Login1Details);

        //---------     BoxLayout for other Login details
        BoxLayout bx_Login2Layout = new BoxLayout(BoxLayout.Y_AXIS);
        Container con_Login2Details = new Container(bx_Login2Layout);
        con_Login2Details.setScrollableY(true);
        con_Login2Details.getStyle().setMargin(5, 5, 10, 10);

        //---------     Login type 3 New Remember Me
        chk_RememberMe = new CheckBox("Remember Me");
        chk_RememberMe.setTickerEnabled(false);
        con_Login2Details.addComponent(chk_RememberMe);

        //---------     Login type 3 New Forgot Password
        btn_ForgotPWD = new Button("Forgot Password?");
        btn_ForgotPWD.setTickerEnabled(false);
        btn_ForgotPWD.setAlignment(Component.LEFT);
        btn_ForgotPWD.setTextPosition(Component.RIGHT);

        con_Login2Details.addComponent(btn_ForgotPWD);

        con_Center.addComponent(con_Login2Details);
        con_Center.getStyle().setPadding(0, 0, 10, 10);

        addComponent(BorderLayout.NORTH, con_Header);
        addComponent(BorderLayout.CENTER, con_Center);

        cmd_Back = new Command("Back");
        this.setBackCommand(cmd_Back);
        addCommand(cmd_Back);
        addCommandListener(this);


        //----------  Add More Command for Menu item
        cmd_AboutUs = new Command("About Us");
        cmd_Privacy = new Command("Privacy");

        addCommand(cmd_AboutUs);
        addCommand(cmd_Privacy);

        //-------       When Login Button is Clicked
        btn_Login.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                     ....
            }
        });
    }
    public void actionPerformed(ActionEvent ae) {
        Command con = ae.getCommand();
        String str_CmdName = con.getCommandName();
        if (str_CmdName.equals("Back")) {
            // here back code 
            frm_Main.setTransitionInAnimator(
                        Transition3D.createRotation(800, true));
            frm_Main.showBack();
        }

        ....
        if (str_CmdName.equals("About Us")) {
            AboutUs aboutus = new AboutUs(frm_Main);
            aboutus.show();
        }

        if (str_CmdName.equals("Privacy")) {
            Privacy privacy = new Privacy(frm_Main);
            privacy.show();
        }

    }
}

EDIT: Added the form's source code

Vimal
  • 1,266
  • 1
  • 9
  • 16
  • There are lot of unknown's and scenarios that can cause this problem ? Please provide your code snippet to acquire more help ? – Vimal Dec 13 '11 at 12:30
  • thanks for replay, i added some code here! plz help me – Harshada Dhamapurkar Dec 13 '11 at 13:56
  • I have edited [my answer](http://stackoverflow.com/questions/8485998/lwuit-need-help-back-command-is-not-working-properly/8489653#8489653). The issue resolution description in in there. – Vimal Dec 13 '11 at 15:41
  • Most welcome, if you were able to resolve your problem, please mark the answer and [accept it](http://stackoverflow.com/faq#howtoask) to help future readers of this posts. – Vimal Dec 15 '11 at 15:42

1 Answers1

2

Got it!

In MainMenu(...) constructor you are storing the reference of SPLASH screen in the class variable frm_Main, this class variable you are passing to the Login(...) constructor in the MainMenu.actionPerformed(...) so effectively you are passing the reference of Splash form object. You need to pass the current class which is MainMenu's reference to the Login(...) constructor and NOT the MainMenu.frm_Main.

Do this, if you don't expect to get back to the Splash screen from MainMenu screen:

public MainMenu(Form form)
{
    this.frm_Main = this;  //by referring to `MainMenu.this` and not input 
                           //parameter `form` which is referring to Splash
                           //object

    ...

If you want to maintain the back reference for MainMenu screen than do this in MainMenu.actionPerformed(...)

Login login = new Login(/*global variable for `MainMenu.this`*/);
Vimal
  • 1,266
  • 1
  • 9
  • 16
  • @HarshadaDhamapurkar please mark and accept this answer, if you feel it helped to solve your problem. – Vimal Dec 16 '11 at 05:17