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

Adobe AIR Development

Reply
Super Contributor
JeffLemon
Posts: 440
Registered: ‎01-13-2011
My Device: Playbook
My Carrier: NA

Remove a class?

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...

--------------------------------------------------------------------------------------------------
BlackBerry Apps: Instruments | ARTPAD | Piano | Drums | Xylophone
Please use plain text.
Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Remove a class?

hey jeff,

 

when you say that your return to the home screen, do you mean when you exit the app completely? or just minimize it?

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Please use plain text.
Super Contributor
JeffLemon
Posts: 440
Registered: ‎01-13-2011
My Device: Playbook
My Carrier: NA

Re: Remove a class?

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?

--------------------------------------------------------------------------------------------------
BlackBerry Apps: Instruments | ARTPAD | Piano | Drums | Xylophone
Please use plain text.
Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Remove a class?

[ Edited ]

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):

 

Event.ADDED_TO_STAGE:

 

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/Event.html#ADDED_TO_...

 

Event.REMOVED_FROM_STAGE:

 

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/Event.html#REMOVED_F...

 

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! :smileyhappy:

 

good luck!

 

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:

 

http://supportforums.blackberry.com/t5/Tablet-OS-SDK-for-Adobe-AIR/Going-back-to-main-page/m-p/71241...

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Please use plain text.