Search code examples
user-interfaceblackberryjava-memobile

Return from a pushed BlackBerry screen to the parent screen


I have a main menu in Blackberry application. I want to access different screens when I enter the menu items and need to come back to the main menu. How can I do it?

Here is what I tried but caught up a runtime exception when pressed Back button:

package com.stockmarket1;


import net.rim.device.api.system.Bitmap;
  import net.rim.device.api.ui.*;
  import net.rim.device.api.ui.container.*;
  import net.rim.device.api.ui.component.*;
  import net.rim.device.api.ui.decor.*;
  import net.rim.blackberry.api.push.PushApplication;

    public class StockMarket extends UiApplication implements FieldChangeListener   {
      public Screen _clientList;
      public Screen _comments;
      public Runnable _popRunnable;

    public static void main(String[] args)
    {
        StockMarket theApp = new StockMarket();
        theApp.enterEventDispatcher();
    }

    public StockMarket() {
        //Code for MainScreen
        final MainScreen baseScreen = new MainScreen();

        baseScreen.setTitle("Colombo Stock Exchange");
        ButtonField clientList = new ButtonField("View Client List", ButtonField.CONSUME_CLICK);
        clientList.setChangeListener(new FieldChangeListener() {

            public void fieldChanged(Field field, int context) {
                pushScreen(_clientList);
                }
        });

        ButtonField comments= new ButtonField("View Comments", ButtonField.CONSUME_CLICK);
        comments.setChangeListener(new FieldChangeListener() {

            public void fieldChanged(Field field, int context) {
                pushScreen(_comments);
                }
        });     
        Bitmap bitmap = Bitmap.getBitmapResource("logo1.png");
        BitmapField logo = new BitmapField(bitmap, BitmapField.FIELD_HCENTER);
        LabelField newLine = new LabelField("\n");

        baseScreen.add(logo);
        baseScreen.add(newLine);
        baseScreen.add(clientList);
        baseScreen.add(comments);


        //Code for _comments
        _comments = new FullScreen();
        _comments.setBackground(BackgroundFactory.createSolidBackground(Color.LIGHTCYAN));
        LabelField title = new LabelField("Comments",LabelField.FIELD_HCENTER);


        LabelField comment = new LabelField("Type");
        RichTextField rtfComment = new RichTextField();

        _comments.add(title);
        _comments.add(comment);
        _comments.add(rtfComment);

        //Code for _clientList
        _clientList = new FullScreen();
        _clientList.setBackground(BackgroundFactory.createSolidBackground(Color.LIGHTBLUE));
        LabelField clientTitle = new LabelField("Listed Companies\n\n", LabelField.FIELD_HCENTER);
        LabelField line = new LabelField("__", LabelField.USE_ALL_HEIGHT);

        ButtonField closeClient = new ButtonField("Close", ButtonField.CONSUME_CLICK);


        closeClient.setChangeListener(new FieldChangeListener() {

            public void fieldChanged(Field field, int context) {
                pushScreen(baseScreen);
            }
        });

        _clientList.add(clientTitle);
        _clientList.add(line);

        //Events
        pushScreen(baseScreen);
    }

    public void fieldChanged(Field field, int context) {
        // TODO Auto-generated method stub

    }
}

Solution

  • The code for your 'Close' button is a problem, but I think you said you get a RuntimeException when hitting the 'back' button on the device, which I think has a different cause.

    Instead of pushing the menu screen onto the screen stack, you should just pop the current screen. This will return to the menu screen that was displayed previously:

        closeClient.setChangeListener(new FieldChangeListener() {
            public void fieldChanged(Field field, int context) {
                // previous code:
                // pushScreen(baseScreen);
                // correct code:
                popScreen(_clientList);
            }
        });