How to navigate back to the previous screen in Blackberry?

前端 未结 2 1794
春和景丽
春和景丽 2020-12-25 10:06

In Blackberry I can navigate from one screen to the next screen, but I can\'t navigate back to the previous screen. Pressing the escape key in the emulator terminates the en

2条回答
  •  醉话见心
    2020-12-25 11:04

    As Andrey said, there is a display stack, so if you push screens without popping them, they will stay in stack, so closing current screen, previous screen will be shown automattically, and if there is no prev. screen, application will close.

    However it's not good to hold many screens in display stack, so you can implement kind of stack inside of screens, to handle navigation manually.

    Abstract screen class for screen stack implementation:

    public abstract class AScreen extends MainScreen {
        Screen prevScreen = null;
    
        void openScreen(AScreen nextScreen) {
            nextScreen.prevScreen = this;
            UiApplication.getUiApplication().popScreen(this);
            UiApplication.getUiApplication().pushScreen(nextScreen);
        }
    
        void openPrevScreen() {
            UiApplication.getUiApplication().popScreen(this);
            if (null != prevScreen)
                UiApplication.getUiApplication().pushScreen(prevScreen);
        }
    }
    

    Sample first screen:

    public class FirstScreen extends AScreen implements FieldChangeListener {
    
        ButtonField mButton = null;
    
        public FirstScreen() {
            super();
            mButton = new ButtonField("Go second screen", ButtonField.CONSUME_CLICK);
            mButton.setChangeListener(this);
            add(mButton);
        }
    
        public void fieldChanged(Field field, int context) {
            if (mButton == field) {
                openScreen(new SecondScreen());
            }
        }
    }
    

    Sample second screen:

    public class SecondScreen extends AScreen implements FieldChangeListener {
    
        ButtonField mButton = null;
    
        public SecondScreen() {
            super();
            mButton = new ButtonField("Go first screen", ButtonField.CONSUME_CLICK);
            mButton.setChangeListener(this);
            add(mButton);
        }
    
        public void fieldChanged(Field field, int context) {
            if (mButton == field) {
                openPrevScreen();
            }
        }
    
        public boolean onClose() {
            openPrevScreen();
            return true;
        }
    }
    

提交回复
热议问题