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

Native Development

Reply
Super Contributor
pg1181
Posts: 398
Registered: ‎01-12-2011
My Device: Q10

How to get a reference to the current Application from anywhere in my code?

[ Edited ]

I would like to know how to get a reference to the current Application through code. I would like to be able to access my NavigationPane on my applications main screen from anywhere in my code so that I can push and pop screens.

 

I guess I'm trying to do something like:

 

getApplication()->navPane->pop();

 

or

 

getApplication()->navPane->push(aDifferentPage);

 

Or am I going about the pushing and popping the wrong way? I undertsand the idea of pushing and popping screens from a NavigationPane, I would just like to know how to do this from any screen.

 

Any ideas??

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: How to get a reference to the current Application from anywhere in my code?

If you look at the class hierarchy, above bb::cascades::Application you'll get to bb::Application and then QCoreApplication, where there's an instance() static method that does what you want.


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!
Please use plain text.
Super Contributor
pg1181
Posts: 398
Registered: ‎01-12-2011
My Device: Q10

Re: How to get a reference to the current Application from anywhere in my code?

[ Edited ]

Thanks!

 

Could you show me an example of how I would create the reference??

 

Something like this??:

 

QCoreApplication *instanceOfApp = QCoreApplication.instance();

 

This doesn't work though.

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: How to get a reference to the current Application from anywhere in my code?

Static methods are called on the class, so you don't need any reference to anything ahead of time.

Just do bb::cascades::Application::instance() and it should give you the reference to the global application object.

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!
Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: How to get a reference to the current Application from anywhere in my code?

Or QCoreApplication::instance() ...

By the way, when reporting that something "doesn't work", please always include details like what error message you see. Otherwise people responding have to guess what you're doing/seeing.

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!
Please use plain text.
Super Contributor
pg1181
Posts: 398
Registered: ‎01-12-2011
My Device: Q10

Re: How to get a reference to the current Application from anywhere in my code?

[ Edited ]

Sorry about not providing more info, it was a syntax error:

 

error: expected primary-expression before '.' token

 

I was trying to use the dot operator where I shoud've used ::

 

It seems to be working so far. Thanks alot for your help!

Please use plain text.