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
Regular Contributor
spardhan
Posts: 64
Registered: ‎01-27-2011
My Device: Not Specified
Accepted Solution

static final constants reinitializing

Hi All,

 

We have a java library project to share common code between all our java-based clients. We have created a "linked src folder" to the library project in our client project in addition to the src folder that belongs to the client project. There is a file in the library that contains "static final" objects that are created when the application runs.

 

The problem is that though the application starts and the static final objects are created as expected, the first time the client code tries to reference one of these objects all the objects get reinitialized again. We are seeing this only on the BlackBerry. The same approach works fine on J2ME and Android.

 

Please advise on how we could solve this issue?

 

Thanks,

Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: static final constants reinitializing

I suspect the problem here is that static values are not shared between different Application instances - and by Application here I mean different instances of the Application class (UiApplication extends Application). 

 

So, for example, if you have an alternate entry point, and a normal entry point, these two will see different instances of static values.  In your case, I suspect the client application and the start-up application are diffrent instances. 

 

If you want to share variables between two different Application instances then the best option is runtimestore:

http://supportforums.blackberry.com/t5/Java-Development/Create-a-singleton-using-the-RuntimeStore/ta...

Please use plain text.
Regular Contributor
spardhan
Posts: 64
Registered: ‎01-27-2011
My Device: Not Specified

Re: static final constants reinitializing

Hi Peter,

 

I am using the runtime store and the implementation or initialization of the application singleton is done exactly as in the link you posted.

 

After debugging it for sometime, I believe I have narrowed down the issue. In our application we like to be notified on the states of a phone call and thus we extended the AbstractPhoneListener. It is only when the first call notification incoming/initiated) is received and a reference to the "static object" made is when all the static objects are reinitialized.

 

Accessing the "static objects" when the phone app is not active does not trigger the reinitialization. I am suspecting that there is an instance of our application in the phone application because the reinitialization happens only once and only when a phone application comes active. Could this be true? If so, how would I work around this?

 

Thanks,

Please use plain text.
Developer
simon_hain
Posts: 15,874
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: static final constants reinitializing

the phone application also has its own process, and the phone listener runs in this context. if you access a static variable from your app it will be not the same as when you access it from your app directly.
my suggestion: don't use static at all if you communicate between processes, just use runtimestore (or global events).
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Regular Contributor
spardhan
Posts: 64
Registered: ‎01-27-2011
My Device: Not Specified

Re: static final constants reinitializing

 

I found a workaround and that is to use invokeLater. If the stattic final constants are called from the run method of a Runnable that is passed to invokeLater the constants are not reinitialized.

 

However the invokeLater is slow. If I were to switch to using global event as an alternative, how fast would that be considering a phone application process is active and we are dispatching a global event from that context?

Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: static final constants reinitializing

If all you are trying to do is access shared data, then RuntimeStore is the way to go and fast. 

 

GlobalEvent involves at least one invokeLater (the event is processed on the Event Thread on the target application) so I would not think that it would be faster than invokeLater.

 

How did you get the application to do the invokeLater against?

Please use plain text.
Regular Contributor
spardhan
Posts: 64
Registered: ‎01-27-2011
My Device: Not Specified

Re: static final constants reinitializing

I am trying to notify another thread in my application about the call states. We notify threads using asynchronous messaging. The static final constants are essentially just the names of those threads

 

My application is a singleton, I retrieved an instance of it and called invokeLater.

Please use plain text.
Developer
simon_hain
Posts: 15,874
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: static final constants reinitializing

basically: don't use static at all with different processes.
you can put a service into the runtimestore that returns the names, for example.

thread notification within a process should be simple, across processes i would suggest global events.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: static final constants reinitializing

Just backing up Simon's comments.

 

You said "My application is a singleton, I retrieved an instance of it and called invokeLater"

 

I presume therefore that you are using RuntimeStore to provide that Singleton.  If you can do that, then you can put anything in there, including the names of the Threads. 

 

Putting your Application in RuntimeStore means that all your Application variables, screens and so on will not be garbage collected when you exit your Application, which might not be a great idea. 

Please use plain text.