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
Developer
Posts: 327
Registered: ‎12-13-2010
My Device: Bold 9900/Z10 Special Edition
My Carrier: Rogers
Accepted Solution

Button event in App Menu

Can I call a function that is in the main App window, from the UI of the App menu with an Event Listener in the Main app listening for a click of a button in the UI of the menu?

Example:

(UI button in swipe-down menu in UI.as)
...
ResetButton = new LabelButton();
ResetButton.label = 'Reset view';
...

(code in MyApp.as)
...
private var ui:UI;
...
public function MyApp()
{

ResetButton.addEventListener(MouseEvent.MOUSE_DOWN, resetView);
...
}

 Where resetView is a function to reset the default view of the app.

 

CEO/Lead Developer - Tensioncore™ Design Studios
http://tncr.ws/ - ShortenThis!™ for Blackberry Playbook | ShortenThis!™ for BlackBerry® 10
The Periodical Toe A highly informative Table of Elements
DrawThis! A fun drawing app for the BlackBerry® PlayBook™!
http://www.tensioncore.com/ - Tensioncore™ Web Hosting
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Button event in App Menu

Sure, the menu isn't really special, just some other DisplayObject, right?

 

And so long as you do it via a well-defined event dispatch/listen arrangement, it's probably even clean.

 

When I close my app menu, it sends an event to anyone listening.  I have my main app class listening, and when it gets the message it "harvests" some preferences settings from my menu and updates itself with those.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
Posts: 1,269
Registered: ‎12-29-2010
My Device: PlayBook, Z10 LE, Dev Alpha C

Re: Button event in App Menu

[ Edited ]

Do you have code that is currently trying to do this and failing?

 

I would think you can bind an event listener to an event dispatcher from anywhere in your app just so long as you have a reference to the event dispatcher in the code where you are calling addEventListener.

 

I.e. as long as in MyApp.as, your MyApp() constructor has a reference to the ResetButton in UI.as, you can make your addEventListener() call:

 

 

 

ui.ResetButton.addEventListner(MouseEvent.MOUSE_DOWN, resetView);

 

where I assume resetView() is a method of MyApp.as and ui gets a reference to your UI.as class.

 

Edit: Obviously ResetButton would have to be public, or protected internal and in the same package as MyApp.as for the above to work.

Developer
Posts: 327
Registered: ‎12-13-2010
My Device: Bold 9900/Z10 Special Edition
My Carrier: Rogers

Re: Button event in App Menu

Alright, now its working almost perfectly, next problem is when I click the reset button when I'm already on the default view, it then clears the screen because of this code:

 

 

this.stack.setPageIndex( 0, PageViewStack.LEFT_RIGHT );
this.setChildIndex( this.ui, this.numChildren-1 );

 

So how can I check the page index to see if it's on the default view before clicking the resest button and having it go -1, and instead have it not change the index?

 

CEO/Lead Developer - Tensioncore™ Design Studios
http://tncr.ws/ - ShortenThis!™ for Blackberry Playbook | ShortenThis!™ for BlackBerry® 10
The Periodical Toe A highly informative Table of Elements
DrawThis! A fun drawing app for the BlackBerry® PlayBook™!
http://www.tensioncore.com/ - Tensioncore™ Web Hosting
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Button event in App Menu

hey nick,

 

im not too familiar with the page stack, but if you want to prevent the indexing from going to negatives you can do this:

 

 

this.stack.setPageIndex( 0, PageViewStack.LEFT_RIGHT );

if (this.numChildren > 0)
{
	this.setChildIndex( this.ui, this.numChildren-1 );
}

 

hope that works. good luck!

 

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
Developer
Posts: 327
Registered: ‎12-13-2010
My Device: Bold 9900/Z10 Special Edition
My Carrier: Rogers

Re: Button event in App Menu

it still produces the same result...it clears the screen
CEO/Lead Developer - Tensioncore™ Design Studios
http://tncr.ws/ - ShortenThis!™ for Blackberry Playbook | ShortenThis!™ for BlackBerry® 10
The Periodical Toe A highly informative Table of Elements
DrawThis! A fun drawing app for the BlackBerry® PlayBook™!
http://www.tensioncore.com/ - Tensioncore™ Web Hosting
Developer
Posts: 1,269
Registered: ‎12-29-2010
My Device: PlayBook, Z10 LE, Dev Alpha C

Re: Button event in App Menu

[ Edited ]

@tensioncore:  PageViewStack.as is storing its current page in the private variable _current_page and at least the version I downloaded, which I think is current, doesn't have a getter function for that variable.  But that is easy to fix.  Just add this code to your local version of PageViewStack.as:

 

 

public function get current_page():DisplayObject {
   return _current_page;
}

 

 

 

and then in your code where you need to check what page your page view stack is currently on, just reference 

 

 

this.stack.current_page

 

 

 

Give that a try & let us know how it goes .. 

Developer
Posts: 327
Registered: ‎12-13-2010
My Device: Bold 9900/Z10 Special Edition
My Carrier: Rogers

Re: Button event in App Menu

Thanks!

 

I ended up doing this on my main App:

if (this.stack.current_page != page0)
{
	this.stack.setPageIndex( 0, PageViewStack.LEFT_RIGHT );
	this.setChildIndex( this.ui, this.numChildren-1 );
}

 

Works like a charm! Smiley Happy

 

CEO/Lead Developer - Tensioncore™ Design Studios
http://tncr.ws/ - ShortenThis!™ for Blackberry Playbook | ShortenThis!™ for BlackBerry® 10
The Periodical Toe A highly informative Table of Elements
DrawThis! A fun drawing app for the BlackBerry® PlayBook™!
http://www.tensioncore.com/ - Tensioncore™ Web Hosting
Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: Button event in App Menu

I added this to the library so when a new release is out, it will be included.  Nothing pressing to do so yet.  Thanks for the feedback.