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
New Developer
Posts: 8
Registered: ‎06-27-2014
My Device: BlackBerry 9360
My Carrier: Movistar
Accepted Solution

App freezes when closing a MainScreen, loses focus and says it has no screens

[ Edited ]

I'm learning the basics for Java development for BlackBerry, the version I use is SDK 6.

 

While working on an example provided at my workplace I came across a bug that causes the app to freeze and stop responding to user input after closing a screen. The console output showed a message saying:

 

 

Foreground app *************** lost focus because it has no screens,

 

which is weird because a screen is still being displayed. After this no interaction can be made with the app, I'm only able to hold the menu button to select go to Home screen but if I try to enter the app is remains frozen in the last window, I even rebooted the device and the app stayed the same (I'm not familiar if this kind of persistent behaviour is normal in BlackBerry development).

 

Only uninstalling and reinstalling the app manages to "solve" this.

 

I checked the example project and there's a class called ScreenManager which is implemented to handle the screens shown on the app. It keeps a Vector of the stack of current screens being displayed. This class is mostly used to handle the trajectories inside the app, i.e. if I performed j action in MainScreen C, instead of returning to MainScreen B, show MainScreen F.

 

Every class that extends MainScreen has its onClose method overriden:

 

    public boolean onClose()  {
        ui.getSm().back(null);
        return true;
    }

 

to call a function named back on this ScreenManager class, which checks the current state of the stack of screens, if the Vector object only has one object then a dialog ask the user what to do, if close the app or stay in app. In case there's more than 1 screen in our Vector object, then we just call the method that performs the popScreen call:

 

    public void back() {
        if(pageStack.size() - 1 == 0) {
            //only one screen in stack, show dialog asking if user wishes to close app
            int choose=Dialog.ask(Dialog.D_YES_NO,
            ui.getRb().getString(ui.EXIT_YES_NO));
            if(choose==Dialog.YES)
            {

                System.exit(0);
            }
        }  else {

            // Method that handles the popScreen call and removes the current screen from our Vector object
            popPageStack();
        }
        
    }

 

The popPageStack method looks like this:

 

    private void popPageStack() {
        pageStack.removeElementAt(pageStack.size()-1); // remove the object that represents the current screen
        UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen()); // remove the actual screen from the application
    }

 

Since this whole logic is called when the onClose method is triggered, I don't understand how I can "run out" of screens, especially since the console is telling there's none and a Screen is still being displayed.

 

What is causing this problem in the app? Is it posible to pop out all of the screen in the app and still be in the app?

 

EDIT


I made a small test project of my own and placed a menu item that when pressed removes the only window I have and the behaviour I described earlier happened.

 

For some reason the test project I'm studying pops out all of its screens when pressing back, but I don't understand how is that even possible. Could it be I'm pressing the back button too fast and somehow it's triggering several pops?

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: App freezes when closing a MainScreen, loses focus and says it has no screens

The situation you have described happens when you pop the last screen on the BlackBerry 'display stack' but do not close the application.  As a result, the Blackberry OS still tries to display something for the application and that is usually the last screen that was displayed. However this is not a real screen, so there is no interaction, in effect looking like the aplication has hung.  It hasn't, it just doesn't have any screens.

 

Now you are going to say that can't happen because you keep a track of the screens in the stack.  But sorry, I would say that is what is happening and there is a bug in the screen stack management.

 

There is, in my opinion, one potential bug in your stack management.  I think you should recover the actual screen from the Vector and pop that screen, not the active one.  You could also check to make sure that the screen you are popping is the active one.  Then you will detect when the screen stack you have goes out of sync with the BlackBerry display stack. 

 

I am not sure why your application is maintaining its own stack anyway, since the Blackberry has a display stack itself.  You can iterate through the BlackBerry display stack using getActiveScreen(), and then getScreenBelow().

 

New Developer
Posts: 8
Registered: ‎06-27-2014
My Device: BlackBerry 9360
My Carrier: Movistar

Yeah I changed the way to remove the current screen, sinc...

Yeah I changed the way to remove the current screen, since thanks to our stack of opened screens I can get the id for the top most screen, then I use that in a switch to know which MainScreen reference to send in the popScreen call, that way it's not something as ambigous as using getActiveScreen.

 

There were also custom PopupScreen objects (that is objects that extend from PopupScreen class) being pushed and popped outside the ScreenManager class, that logic was being implemented inside each class, and there too getActiveScreen was used to pop out the PopupScreens but that was in my opinion a bad way to do it, because doing that is like hoping for the PopupScreen to be the one being active, instead I changed that to popScreen(CustomPopup.this).

 

Gotta test some more to see if this solves the issue at hand, but since the way to replicate it is so erratic then I have to wait.

 

Any more you can advice me?

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Yeah I changed the way to remove the current screen, sinc...

One other thing you might consider doing is logging your actions and perhaps the 'stack' at various points, such as when you push and pop screens. 

New Developer
Posts: 8
Registered: ‎06-27-2014
My Device: BlackBerry 9360
My Carrier: Movistar

Re: App freezes when closing a MainScreen, loses focus and says it has no screens

Debugging is a headache for me mostly because the breakpoints stop firing after a few tests, but yeah thanks for the insight I haven't come across this problem so it might be solved by now but who knows, I'll wait for the tester to tell me if this bug happened again.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: App freezes when closing a MainScreen, loses focus and says it has no screens

For the debugging to work, the app being debugged and the version used in Eclipse must match, otherwise breakpoints are often missed.  Possibly you need to update the app on the phone to make sure breakpoints continue to be hit?