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
Developer
Posts: 64
Registered: ‎09-02-2008
My Device: 8830, 8100

Static behavior for background

Hi!

 

I have discovered an unexpected behavior for static objects belonging to a class when an application is put in the backgound due to a call to requestBackground() method ... If I have a not null instance object but set as static in another class, next time the application goes to foreground that object is set to null... If I keep a reference of that object in a not-static object, then that reference is not null when the application comes to foreground ... I hope you understood me... If not, here is an example:

 

class A{}

class B{

static A obj;

private void meth(){

obj = new A();

}

}

 

Suppose meth() gets called before the application is put to background, after the application comes to foreground, obj is null...

 

Did any of you experience this issue? If so, can I find something helpfull in the technical knowledge center or someplace else?

 

Best regards!

gunar

New Developer
Posts: 22
Registered: ‎08-13-2008
My Device: Not Specified

Re: Static behavior for background

I've run into that too - think it has something to do with the way RIM doesn't marshal object data across threads/processes. It seems to be a problem with class objects only, since basic variables such as ints seem to work as you'd expect. I ended up using RuntimeStore.getRuntimeStore().put()/get() to track the object.

Developer
Posts: 64
Registered: ‎09-02-2008
My Device: 8830, 8100

Re: Static behavior for background

Hi!

 

Thanks for the reply! I am keeping the static references in an object instance ... but you don't want to see the code ....  Smiley Sad

 

Best regards!

gunar

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

Re: Static behavior for background

You need to be aware that a Static is not the same as a Singleton.  This is specially important in the Rim environment, where the JVM creates a new instance of your Application, with new statics, for each 'startup' method you have.  So if you have an alternate entry, statics used by the alternate entry will not be the same as statics used by the other entry points.

 

As ehartwell notes, the best way to get a Singleton, which is typically what people expect to get when they code a static, is to use RuntimeStore.

 

That said, I have not seen the problem initially reported, i.e. a requestbackground() causing a loss of the references to static objects.  I was going to test this for myself, but have not had time.  Can you confirm that you are NOT using an alternate entry, i.e. you only have a user interface and no startup processing?

Developer
Posts: 64
Registered: ‎09-02-2008
My Device: 8830, 8100

Re: Static behavior for background

Hi!

 

I have an application which is set from JDE to auto-run at device startup and I am not sending other arguments to the main(String [] args) method, so there is no alternate entry point... When debugging this issue, I noticed that these objects are created every time the device goes to foreground.

 

Br,

gunar

 

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

Re: Static behavior for background

[ Edited ]

Sorry not tested yet. 

 

When you say "goes to foreground" do you mean:

a) when you click on the icon and so start your GUI processing

b) you click on the icon after your GUI processing issues an Application.requestBackground().

 

If (a), then I believe you are, from a BlackBerry JVM perspective, starting two different Applications and my understanding (and experience) is that you that you will get a second set of statics.

Message Edited by peter_strange on 09-22-2008 09:01 AM
Developer
Posts: 64
Registered: ‎09-02-2008
My Device: 8830, 8100

Re: Static behavior for background

Hi!

 

I am in case a).

In this case why only the set of statics is reset and not all applications objects? What is the reason for this?

 

Br,

gunar

BlackBerry Development Advisor
Posts: 15,809
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Static behavior for background

Each process executes static constructors and initializes static variables once when it is first run in that process.  Listeners often run in a different process from your application and each initializes static members.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Static behavior for background

This question, or a related one, gets asked from time to time, and it is quite confusing (at least to a simple soul like me). 

 

To help (I hope), I've knocked up the following little test program.  This program was set to autostart, and is also started with an icon.  Have a look at the program, predict what you think will be output at startup time and when the icon is clicked (especially think about the static counters and Object references), then look at the output and see if you were right.  Note that I've also added the results to a screen, so you can try this on a real device.  One other thing to note is that you will see a 'background' screen (press the task-switch combination of ALT-Escape) and also, once your click on the icon, a foreground screen.  Try it and see what you see on each of the screens too. 

 

The overall point here is that the autostarted process and the icon started process are really independent.  Secondary point - static does not mean Singleton - use RuntimeStore.

 

Good luck figuring it out....

 

public class GUITest extends UiApplication { private static long _staticStartUpTime = 0; private long _instanceStartUpTime = 0; private static Long _staticStartUpTimeLong = null; private Long _instanceStartUpTimeLong = null; private static int _staticCounter = 0; private int _instanceCounter = 0; private static Integer _staticCounterInt = null; private Integer _instanceCounterInt = null; public GUITest() { _instanceStartUpTime = System.currentTimeMillis(); _instanceCounter++; if ( _instanceStartUpTimeLong == null ) { _instanceStartUpTimeLong = new Long(_instanceStartUpTime); } if ( _instanceCounterInt == null ) { _instanceCounterInt = new Integer(_instanceCounter); } MainScreen screen = new MainScreen(); screen.setTitle(new LabelField("GUI Test", LabelField.USE_ALL_WIDTH)); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS"); BasicEditField [] _befArray = new BasicEditField [] { new BasicEditField("Static Time: ", sdf.formatLocal(_staticStartUpTime)), new BasicEditField("Instance Time: ", sdf.formatLocal(_instanceStartUpTime)), new BasicEditField("Static Time Long: ", sdf.formatLocal(_staticStartUpTimeLong.longValue())), new BasicEditField("Instance Time Long: ", sdf.formatLocal(_instanceStartUpTimeLong.longValue())), new BasicEditField("Static Counter: ", Integer.toString(_staticCounter)), new BasicEditField("Instance Counter: ", Integer.toString(_instanceCounter)), new BasicEditField("Static Counter Integer: ", Integer.toString(_staticCounterInt.intValue())), new BasicEditField("Instance Counter Integer: ", Integer.toString(_instanceCounterInt.intValue())) }; System.out.println("**** ADDING FIELDS TO TEST SCREEN ****"); for ( int i = 0; i < _befArray.length; i++ ) { screen.add(_befArray[i]); System.out.println(_befArray[i].getLabel() + _befArray[i].getText()); } System.out.println("**** ADDED FIELDS TO TEST SCREEN ****"); pushScreen(screen); } public static void main(String[] args) { _staticStartUpTime = System.currentTimeMillis(); _staticCounter++; if ( _staticStartUpTimeLong == null ) { _staticStartUpTimeLong = new Long(_staticStartUpTime); } if ( _staticCounterInt == null ) { _staticCounterInt = new Integer(_staticCounter); } GUITest instance = new GUITest(); instance.enterEventDispatcher(); } }

 

 

Results from 'startup.

 

**** ADDING FIELDS TO TEST SCREEN ****
Static Time: 00:25:46.888
Instance Time: 00:25:46.948
Static Time Long: 00:25:46.888
Instance Time Long: 00:25:46.948
Static Counter: 1
Instance Counter: 1
Static Counter Integer: 1
Instance Counter Integer: 1
**** ADDED FIELDS TO TEST SCREEN ****

 

Results from subsequently pressing the icon associated with this applicaiton.

 

**** ADDING FIELDS TO TEST SCREEN ****
Static Time: 00:26:48.216
Instance Time: 00:26:48.226
Static Time Long: 00:26:48.216
Instance Time Long: 00:26:48.226
Static Counter: 1
Instance Counter: 1
Static Counter Integer: 1
Instance Counter Integer: 1
**** ADDED FIELDS TO TEST SCREEN ****