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: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Add a field to a Screen more than once

While Peter's approach is the surest cure to your problems, even if you employ that, add some debug prints (such as printing m.getClass().toString() at least) to find out the actual reason. His code is treating the symptoms, but eventually you'll have to find the root of the problem and fix that once and for all.

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 511
Registered: ‎05-31-2009
My Device: Not Specified

Re: Add a field to a Screen more than once

Nothing's too hokey Peter. But what does 'previous instance still active' mean when I exit?

 

Justin

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

Re: Add a field to a Screen more than once

That is one of those things that at one point I think I understood, but I don't now....

 

Worthy of a new Thread?

Developer
Posts: 511
Registered: ‎05-31-2009
My Device: Not Specified

Re: Add a field to a Screen more than once

Hey. The class is a VerticalFieldManager. It seems that when you add an ObjectListField to a MainScreen, you actually add a VFM with an OLF...

 

The VFM has a manager which is a TitleStatusManager. That has a manager which is the MainScreen.

I really don't know where to look to understand it. My code below is as much as I understand now. It gets an IllegalArgumentException deleting the VFM from the TitleStatusManager.

 

 

                                  SRSS_OLF myOLF = (SRSS_OLF)m_everyOLF.elementAt(i);
                                  
                                  Manager m = myOLF.getManager();
                                  if (m != null) {
                                  
                                    m.delete(myOLF);   /// m is a VFM    
                                    Manager mm = m.getManager();
                                    
                                    if (mm != null) 
                                        mm.delete(m);   /// mm is a TitleStatusManager (exception on this line).  The next level above mm is the MainScreen (RepMS)
                                  }

                                  m_sharpRSSMainScreen.add(myOLF);

 

 

 

snap.jpg

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

Re: Add a field to a Screen more than once

From memory, a MainScreen actually has 3 managers under the covers, that you normally don't see.  There is the status area Manager, the actual screen (the delegate Manager) and the Title area Manager.  I think these are all VFMs or extensions there-of. 

 

However if you use a MainScreen normally, you don't need to see this and you shouldn't have too.  I suspect arkadzy is right suggesting you should look at the code that is supposed to remove these Fields, rather than band-aid fixing the problem.

 

OK, here is another suggestion.  Instead of worrying about all the Fields, why not add them all to a VFM, then just move the VFM from one screen to the next?

Developer
Posts: 511
Registered: ‎05-31-2009
My Device: Not Specified

Re: Add a field to a Screen more than once

Hey. Mistake in my code sample. App name is changing from stunningrss to sharprss. They're the same in the code :/

J.
Developer
Posts: 511
Registered: ‎05-31-2009
My Device: Not Specified

Re: Add a field to a Screen more than once

Hi JMarc. Is the exception coming from the mainscreen, which should delete individually, or the olf fields which already have managers? The mainscreen is a new instance of course on app open. Appears to be in the added fields, so would delete() still work? Great to have more info on these managers but from the field side. Cheers, J.
Developer
Posts: 94
Registered: ‎10-01-2009
My Device: Not Specified

Re: Add a field to a Screen more than once

Hi Justin. I don't understand your comment about the VFM. You said, "when you add an ObjectListField to a MainScreen, you actually add a VFM with an OLF". I don't think that's it. When you invoke MainScreen's add method to add the OLF, you are actually adding the OLF to a VFM the MainScreen already owns. I would guess a MainScreen or any Screen would get a mite peeved if you try to delete one of its Managers. So your comment "an IllegalArgumentException deleting the VFM" is not surprising.

 

But that's different from the IllegalState exception you originally asked about. That comes from an OLF when you try to add it to a Manager when it thinks it already has one.  I originally thought that happened because add() adds to the MainScreen's delegate Manager but deleteAll() deletes from the MainScreen itself, so the OLF never got deleted. But now you've made it clear that it's not even the same MainScreen!

 

So here's another try: You added the OLFs to a VFM owned by a MainScreen, then closed the MainScreen. The VFM can't get garbage-collected because the OLFs still have a reference to it -- that's what getManager() returned. So when you try to add the OLF to a new MainScreen, the OLF thinks it is still managed.

 

The flaw in that reasoning is that it assumes you have the same OLF. I thought you were creating a new one from persistent store.  Can you clarify that?  I don't know what you meant by "previous instance still active", either, but it's clear that something is hanging around when you don't expect it to.

 

If you do have the same OLFs, Peter's defensive programming should take care of the problem.  (And arkadyz is right -- defensive programming often masks the root cause of the problem it defends against. But it sounds nicer than "hokey".)  It asks each OLF if it is already managed, and if so, tells the Manager to release the OLF.  Then you can add the OLF to a new Manager (the new MainScreen's delegate VFM).  In your version, you then go on to delete the Manager's Manager, which is not needed and does not work.

 

If this chain of reasoning is somewhere close to right, another way is to delete the OLFs when you close the old MainScreen -- maybe put a loop in its onClose() method.  Come to think of it, that might be a good thing to do anyway, whether I have it right or not.  Somehow we seem to have fallen into C++ do-it-yourself memory management.

Developer
Posts: 511
Registered: ‎05-31-2009
My Device: Not Specified

Re: Add a field to a Screen more than once

Hey thanks for that.

"The flaw in that reasoning is that it assumes you have the same OLF. I thought you were creating a new one from persistent store.  Can you clarify that?  I don't know what you meant by "previous instance still active", either, but it's clear that something is hanging around when you don't expect it to.

If you do have the same OLFs, Peter's defensive programming should take care of the problem."

Ok: the olfs are created once and added to the runtime store for fast retrieval (a bunch of 10 olfs takes nearly 30 seconds to create- might chop that down but its too slow). They are retrieved every time the app is opened or the screen containing the olfs is changed. Peter's code indeed stopped the illegalargexception. But now this 'prev instance still acvtive' popup (says it twice) comes up when you open the app the second time. So its not fully right of course. I don't know quite what this message means.

Thanks for your time and detail
Developer
Posts: 511
Registered: ‎05-31-2009
My Device: Not Specified

Re: Add a field to a Screen more than once

Hi folks- update: the message Previous instance still active is not an exception. In the log it says

System- StunningRSS still active but it is not in bold. Click on it and it says Severity: always logged. Maybe its just for my information? In that case all I'd need to do would be to suppress the popup...

J