Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Developer
Posts: 28
Registered: ‎09-01-2009
My Device: Not Specified
Accepted Solution

Problem with switching screens

Hello, I have a problem with switching screens. Here's my code (both screens extends MainScreen):

 

public class Main extends UiApplication {

   private TrainLinkScreen trainLinkScreen;
   private WorkingScreen workingScreen;
   private String message;

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

   public Main() {
      workingScreen = new WorkingScreen();
      pushScreen(workingScreen);

      try {
         trainLinkScreen = new TrainLinkScreen();
      } catch (GpsException ex) {
         message = ex.getMessage();
      } catch (FileReaderException ex) {
         message = ex.getMessage();
      }
      if (message != null && !message.equals("")) {
         invokeLater(new Runnable() {

            public void run() {
               Dialog.alert(message);
               System.exit(1);
            }
         });
      } else {
         popScreen(workingScreen);
         pushScreen(trainLinkScreen);
      }
   }
}

 

The problem is, that the workingScreen doesn't show. The downloading takes time a bit, so I wanted to put there a screen like "I'm working, please wait....", but it doesn't show, the device freezes for a few seconds and then the trainLinkScreen appears. Could somebody please write where's the problem? I tried invokeLater on the first screen, but it ends with crush on the popScreen(trainLinkScreen). I tried invokeLater on the trycatch block too, but it doesn't work either. Please help.

Developer
Posts: 157
Registered: ‎05-10-2009
My Device: Not Specified

Re: Problem with switching screens

Probably you are doing a little too much in your constructor.  Instead, put the "I'm working" status into a different method.  the IamWorking() method first updates the screen with something to inform the user (a textfield or a guagefield for example), after that, put the other 'stuff' from your constructor which is taking so much time.   Then, before enterDispatcher, explicitly call IamWorking.

  

Developer
Posts: 28
Registered: ‎09-01-2009
My Device: Not Specified

Re: Problem with switching screens

Ok, I now have the iAmWorking() method, which contains the workingScreen initialization and shows it; and an initialize() method, which contains the other "stuff" from the constructor. And in the main method I call:

 

 public class Main {
   public static void main(String[] args) {
      TrainsUiApplication tua = new TrainsUiApplication();
      tua.iAmWorking();
      tua.enterEventDispatcher();
      tua.initialize();
   }
}

 

the workingScreen shows, but there's nothing going on after that Smiley Sad Could you please tell me where's the problem? Thanks.

Developer
Posts: 157
Registered: ‎05-10-2009
My Device: Not Specified

Re: Problem with switching screens

Maybe call initialize before entering the dispatcher like this:
 

 public class Main {
   public static void main(String[] args) {
      TrainsUiApplication tua = new TrainsUiApplication();
      tua.iAmWorking();

      tua.initialize();
      tua.enterEventDispatcher();
   }

} 
Developer
Posts: 28
Registered: ‎09-01-2009
My Device: Not Specified

Re: Problem with switching screens

This has the same effect as the first case, I see no screen for a 20 seconds and then I see the trainLinkScreen, without the workingScreen even showing... Smiley Sad

If I use invokeLater() on initialize() method, then the first screen displays correctly, but then, If I correnctly understood, I can not use a locationProvider in it because the method getLocation() throws an exception: "getLocation() method cannot be called from event thread". I don't know what to do Smiley Sad

Developer
Posts: 157
Registered: ‎05-10-2009
My Device: Not Specified

Re: Problem with switching screens


Without seeing the code for initilize and the working method, it is hard to know the problem.  Without seeing that code, I would hazard a guess that you are doing too much in your initilize method and you are updating the working status thingy from that method.
 
Basically, if your app takes some time to set things up and you want the user to look at something while they wait, like a guagefield or something, then you put the working status thingy (guagefield, textfield, etc) on the screen and update it periodically from your time-consuming init code.  Once the time consuming operation is complete, then you can add other screens.   If you add the screens too soon, the user never sees the status thingy. 
Developer
Posts: 28
Registered: ‎09-01-2009
My Device: Not Specified

Re: Problem with switching screens

Thank you. I already find out the solution (I hope Smiley Very Happy), I put getting device gps location into a separate method (it's a small part of code, which doesn't take too much time to finish), called it before initialization and the initialization is called in the invokeLater method as a thread (the big part, too much downloading from the internet). The displaying works perfectly this way (first, the "downloading" screen appears and then the rest of code comes) although some other things seems to not work properly now Smiley Very Happy