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
New Developer
epapyrus
Posts: 15
Registered: ‎12-20-2009
My Device: Not Specified

Managing fields

From this post , BBDeveloper said "Notice that only one manager is allowed to control any single field, and a field can never manage itself.". 

 

Does this mean that once I add a field to a Manager, and that manager to a screen, that when I pop that screen off the screen stack, the field is still assigned to/controlled by that manager, even though it is no longer displayed?

 

If that is the case, do I need to explicitly delete managers from screens prior to closing them/poping them from the stack if I want to be able to re-generate that screen later?

 

 

*** A Code expample...

 

By doing this:

 

UiApplication.getUiApplication().pushModalScreen( new AddItemScreen( ) );

 

 

when I close out the AddItemScreen(), are any managers/fields/etc. which were added to that screen also discarded?

 

If not, when I push AddItemScreen() on the stack again, how can I re-use the managers which were created the first time?

 

Thank you in advance.

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

Re: Managing fields

[ Edited ]

"Does this mean that once I add a field to a Manager, and that manager to a screen, that when I pop that screen off the screen stack, the field is still assigned to/controlled by that manager, even though it is no longer displayed?"

 

Yes it does.

 

"do I need to explicitly delete managers from screens prior to closing them/popping them from the stack if I want to be able to re-generate that screen later?"

 

Let me answer that question another way.  If you just want to display the same screen again, you can just push the screen again assuming you have a reference to that Screen.

 

Now on to your example.

 

In this case, you are creating the Screen using

 

new AddItemScreen( )

 

So you have created a new instance of your Screen.  Presumably in your Constructor for that screen, you have created new Fields and added these to a new Manager and then to the screen.  You never specifically have a reference to the screen, so once control returns to the statement after the pushModal, the Screen and all the Field and Managers used in it become dereferenced (and will be garbage collected).  So you can't actually reuse any of these Fields again.

 

So if you wish to reuse the Screen and its contents, save a reference to the Screen.  For example:

 

if ( addItemScreen == null ) {

    _addItemScreen = new AddItemScreen( );

}

UiApplication.getUiApplication().pushModalScreen( _addItemScreen );

 

Please use plain text.
New Developer
epapyrus
Posts: 15
Registered: ‎12-20-2009
My Device: Not Specified

Re: Managing fields

[ Edited ]

@peter_strange,

I've been coding with the expectation that once a screen has been popped off the stack, that all the fields and managers which had been added to that screen were dereferenced as well and ready to be garbage-collected and that a new instance of that screen would have new instances of fields and managers as well. If this is not the case (as you described), then I'll have to recode how my application preps different screens.

 

You say, "if you wish to reuse the Screen and its contents...", but I don't think I do: each time I call AddItemScreen(), I'm creating a new Item which the user will provide a name and attributes for, then that Item gets added to an application-wide list. 

 

I'll play around with my code and see if there's an alternate method accomplishing what I need. I imagine I'll be back with additional quesitons.  :-/

 

****

UPDATE:  Right, so after creating an instance of AddItemScreen() and re-pushing that, the user-entered values of the previous Item's fields (name, attributes) are used as the defaults rather than the default values of the fields of a "new Item()":

 

Psudo-Code Example:

Item () {

   _name = "new item";

   _attribute0 = "1";

   _attribute1 = "red";

}

 

 

private AddItemScreen _ais = new AddItemScreen();

 

pushModalScreen(_ais); // first call to _ais

 

// prompted for:

// name: "new item"

// attribute 1: "1"

// attribure 2: "red"

 

// user enters:

// name: "foo"

// attribute 1: "9"

// attribute 2: "yellow"

// name, attributes saved

 

// ModalScreen returns, other actions take place

 

pushModalScreen(_ais); //second call to _ais

 

// prompted for:

// name: "foo"

// attribute 1: "9"

// attribure 2: "yellow"

 

/* HALT */

 

For a good user experience, I want the prompted-for values to be the defaults provided by Item. And it seemed the best way to do this was to create a new instance of AddItemScreen() each time.  But since this is not working as expected or as needed, what other approaches are available? 

 

Thanks.

 

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

Re: Managing fields

I have no problem with creating a new Screen instance, with new Fields and managers each time you want the user to put values in this. 

 

There could be 'wastage' in this process, in that if you know you are going to do this a number of times, then by creating a new Screen each time that is used once and discarded, you have more work for the garbage collector to do.  But if you create an instance of the Screen and reuse it, this means that the memory footprint of your application has increased because you always have the Screen object, even when not displaying the screen.  There is no right answer.

 

Personally I tend to create a Screen once, and have a 'reset' method I call on the screen to reset values to their default.

 

Now to the points you have raised:

 

"For a good user experience, I want the prompted-for values to be the defaults" -  some would argue that retaining the previous values is actually a better user experience, since if someone is going through adding a number of similar items, this means that some rekeying is saved.

 

"it seemed the best way to do this was to create a new instance of AddItemScreen() each time.  But since this is not working as expected"  - Your previous code should have worked OK.  What was wrong with it?

 

"what other approaches are available?" - the reset() method, as I use, is one way I guess.

 

 

Please use plain text.