Thank you for visiting the BlackBerry Support Community Forums.
BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)
BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.
"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."
- Kevin Michaluk, Founder, CrackBerry.com
Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.
02-19-2011 10:07 AM
I initiate a class and then when my app is finished and you return to the home screen, if you start the app again and reinitiate the app, there are two instances of the class.
Is there anyway to get rid off the class if it allready exists?
Ive tried removeChild and class = null.
But no luck...
02-19-2011 10:08 AM
when you say that your return to the home screen, do you mean when you exit the app completely? or just minimize it?
02-19-2011 10:11 AM
Oh I mean i have a main menu class and an main app class, the main menu class adds the main app, when your done you return to the main menu but clicking on 'start app' again results in 2 intstances of the main app class being present (the old one and the new one).
Just wondering how I can remove the old one?
02-19-2011 10:22 AM - edited 02-19-2011 10:29 AM
ahh ok, then what you want to do (what i do anway) is reuse the main app class. in AS3 its recommended you reuse objects instead of creating new instances.
if you really want a new instance, what you need to do is "destroy" your old main app class. there is no one function to do this (unless you create it yourself of course). by destroying a class i mean remove all the children objects and to set all of its variables to null and splicing any arrays and what not. this approach can be a little tedius and sometimes you miss something. so that leads to option two.
option two you reuse the class. instead of setting it to null, just simply remove the child main app. and when you need it again, you can simply just add the same instance back. all the while you can have two event listeners (one active at a time). One event listener you would have before adding the class that listens for the Event.ADDED_TO_STAGE event and another one once the class is already added and in focus, the Event.REMOVED_FROM_STAGE event.
so when the class is added, the Event.ADDED_TO_STAGE event would be dispatched and you can simply set the new values, just as you would the way you have it. and then add the Event.REMOVED_FROM_STAGE event listener. so once your main menu gets rid of the main app instance via the removeChild() method, that event is triggered and whatever listener funciton you have set up for it (e.g. onRemoved(e:Event)) would be triggered as well and thats where you would do hte clean up so when the user adds that same instance back, it will be ready to add the new variables.
it all may seem a little involved, but in my opinion, reusing objects is the way to go the main reason being i've read that the AS3 garbage collector that cleans up after you are done with an object is inefficient and very random at times and we should rely on it. so i always assume it wont be doing its job when i need it most (albet it a little cynic lol).
so the to summarize: Use the following events (removing one when not in use and applying the other):
and resuse any object or variables that you can by finding a way to "reset" them. it will take some time to get used to but when you get it to work, you'll never go back i promise!
Reuse, reuse, reuse!
EDIT: There is also a third option i forgot to mention. Singleton classes where only one instance will exist of that class througout your entire program. I usually use this approach whenever i need one object (like the navigation system) available throughout my entire application. I use option two for my classes. more information regard the creation of singleton classes in as3 is explained here by jtegen: