views:

2635

answers:

5

Hi,

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 entire application. Is there any other key in the emulator to go to the previous screen or any code to navigate back? If you know please help me.

+7  A: 

The BlackBerry maintains a stack of screens; the display stack.

Screens are popped onto the stack, and popped off the stack through the UiApplication in charge of them. Popping the last screen off the stack closes the application by default.

If you are running a UiApplication, named MyUiApplication, you can add the screen to the stack by calling pushScreen(new SomeScreen());

The screen, if derived from MainScreen, as most BlackBerry screens are, is created with the DEFAULT_CLOSE flag, meaning that the ESCAPE button on the BlackBerry will naturally close the screen, causing popScreen() to be called. You can, of course, call popScreen() following any keypress or trackwheel/trackball click. The screen can also call close() on itself, which has the same result; the screen is popped off the stack, returning the application to the previous screen, or terminating the application if the last screen is popped off the display stack.

If the application is not created as a UiApplication, or if the screen was initially pushed onto the display stack from a non-UI thread (such as a background thread), then one must make sure that the call to close the screen is also done from the UI thread. This can be done by making sure that the eventLock is taken on the Application class prior to performing any UI operation (one would typically call invokeLater as well, in this situation).

If the original screen was popped onto the stack as a global screen (modeless, on top of all other screens), then it must be popped off the stack using something like:

Ui.getUiEngine().dismissStatus(this);

In any case, overriding onClose() and close() of the derived Screen will allow you to trap the occurring exception for debugging and further analysis.

Andrey Butov
+9  A: 

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;
    }
}
Max Gontar
A: 

Brian , have u managed to create ur application with screen navigation. If you have could you please post some sample code or sample application that demonstrates the use of multiple screens and navigation between them.. I am pretty new to blackberry application development and i need to create an application in short notice..So it would be great if u could help me out.. Thanks..

A: 

thanku very much buddy it realy helped me

Sumeet
A: 

Yes, it really helped me too. Thank you very much.

rosethecho