0

I'm developing an app for Blackberry on OS 5. I'm trying to have some previously initialized variables accessed when a call is successfully made. However, the variables don't hold their value and I can't find why.

When the app gets started, testVariable is set to 5, everything works as expected. However, when I try to access the variable from the callConnected event, it is back at 0. Even if I put the variable inside the PhoneEvents class it still goes back to 0. It has one value for PhoneEvents and another for the screen. Shouldn't these variable have only one value given that it's not an instance variable? What am I doing wrong? Is there any other way to have a variable accessed from the PhoneEvents class and the TestScreen class and having it keep its value? Thanks in advance.

    public class TestApp extends UiApplication {
        public TestApp() {
            TestScreen screen = new TestScreen();
            UiApplication.getUiApplication().pushScreen(screen);
        }

        public static void main(String[] args) {
            Phone.addPhoneListener(new PhoneEvents());
            TestApp app = new TestApp();
            app.enterEventDispatcher();
        }
    }

    // The main screen.
    public class TestScreen extends MainScreen {
        public TestScreen() {
            this.setTitle("Test");
            GlobalClass.testVariable = 5;
        }
    }

    // A public static holding a variable I want to have access to.
    public class GlobalClass {
        public static int testVariable;
    }

    // The method that gets called when a call is made.
    public class PhoneEvents implements PhoneListener {
        public void callConnected(int callId) {
            int x = GlobalClass.testVariable;
        }
    }
Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
A. Felix
  • 3
  • 1

2 Answers2

0

This is just an alternate way to do it. Make the GlobalClass a Singleton. This will guarantee that you will get the same instance of the class every time.

Here is a link which shows how to make singletons.

Singleton with Arguments in Java

This Wikipedia link will give an in depth explanation about Singletons. Hope this helps

http://en.wikipedia.org/wiki/Singleton_pattern

Community
  • 1
  • 1
kensen john
  • 5,439
  • 5
  • 28
  • 36
  • It still doesn't keep the value. Nice approach though, might be using this for other projects. – A. Felix Mar 30 '11 at 18:16
  • Singletons wont work, since they are on different processes. Blackberry architecture is fundamentally wrong... :( – lithium Sep 02 '11 at 15:24
0

PhoneListener callbacks run in a different process. You notice this for example by calling UiEngine.getUiEngine().getActiveScreen(), which returns phone screen when you call it from PhoneListener callbacks and application screen when you call it before registering PhoneListener. Also static variables are different.

One solution is to put your application data to the RuntimeStore:

class ApplicationData
{
    public int testVariable;
}

ApplicationData appData = new ApplicationData();
appData.testVariable = 5;

RuntimeStore.getRuntimeStore().put(MY_DATA_ID, appData);

public class PhoneEvents implements PhoneListener {
    public void callConnected(int callId) {
        ApplicationData appData = (ApplicationData) RuntimeStore.getRuntimeStore().get(MY_DATA_ID);
        int x = appData.testVariable;
    }
}
  • Thanks! That's exactly what I ended up doing but it kept bugging me to know if there was something else I was missing. It's good to get confirmation. – A. Felix Apr 18 '11 at 19:33