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

Web and WebWorks Development

Reply
Developer
Posts: 59
Registered: ‎06-08-2011
My Device: n/a

Managing the onForeground listener

If the onForeground listener is registered when my application is opened do I need to check if it has already been registered to avoid a memory leak? Do I need to unregister it when my application is closed? I am not sure how to check if it gets registered more than once.

 

ex. blackberry.app.event.onForeground(onForegroundCallback);

 

https://origin-bdsc.webapps.blackberry.com/html5/apis/blackberry.app.event.html#.onForeground

 

I am not quite understanding how it works. Thanks in advance for your help.

Retired
Posts: 1,382
Registered: ‎07-02-2009
My Device: BlackBerry Bold 9900
My Carrier: Bell

Re: Managing the onForeground listener

Hi songdawg,

 

I assume you are referring to using this method with the Smartphone SDK?  I have personally never unregistered this API when closing an app.

 

If you want to see the Java code that is being executed within this extension, you can do so by browsing Github. Here is the source for the blackberry.app.event API:

 

https://github.com/blackberry/WebWorks/blob/master/api/app/src/main/java/blackberry/app/AppExtension...

 

I see in here that the WeakReference object is being used.  This is intented to avoid memory leaks, so that when the app closes, the garbage collection mechanism can properly clean up the memory used by this API.

 

 

So to summarize - no you don't need to forcibly unregister the onForeground listener.

Follow me on Twitter: @n_adam_stanley
-------------------------------------------------------------------------------------------------------------------------
Your app doesn't work? Use BlackBerry remote web inspector to find out why.
Developer
Posts: 59
Registered: ‎06-08-2011
My Device: n/a

Re: Managing the onForeground listener

Does that mean that if I register it more than once that there is only one instance of it in memory? I register it in my load function which is called every time the application is opened. If the application is uninstalled do you know if it is cleaned up?

 

blackberry.app.event.onForeground(onForegroundCallback);

 

Retired
Posts: 1,382
Registered: ‎07-02-2009
My Device: BlackBerry Bold 9900
My Carrier: Bell

Re: Managing the onForeground listener

So after some deeper discussion with the webworks development team, my recommendation has changed.

 

You *should* be un-registering this listener by setting it to null when you no longer need it (such as during the application's exit step).

 

Why?  Apparently there is a limitation on certain internal resources (relating to XHR) that get tied up.  Garbage Collection *should* tidy these resources up eventually, but managing this propery will help improve the quality of performance of your app.

 

 

songdawg - to answer your question "Does that mean that if I register it more than once that there is only one instance of it in memory?"

 

No there is only one callback associated with the onForeground event.

Follow me on Twitter: @n_adam_stanley
-------------------------------------------------------------------------------------------------------------------------
Your app doesn't work? Use BlackBerry remote web inspector to find out why.
Developer
Posts: 59
Registered: ‎06-08-2011
My Device: n/a

Re: Managing the onForeground listener

[ Edited ]

Thanks for looking into this more deeply. I wish I had a better handle on how to understand these things myself. Can you please have a look at this code and let me know if this is a good way of managing the listeners?

 

 

//In index.html
<script type="text/javascript">
   function ready() {
      initializeApplication();
      window.removeEventListener("DOMContentLoaded", ready, false);
   }
   window.addEventListener("DOMContentLoaded", ready, false);
</script>



function initializeApplication() {
    //here's where the logging and database are initialized
    //first time opened the data is initially downloaded and inserted into sqlite
	
//register events
blackberry.app.event.onBackground(onBackgroundCallback);
blackberry.app.event.onExit(onExitCallback);	
	   
}

function onBackgroundCallback(){
  blackberry.app.event.onForeground(onForegroundCallback);
  
  blackberry.app.event.onBackground(null);
  blackberry.app.event.onExit(null);

  blackberry.app.requestBackground();

}

function onExitCallback(){

  blackberry.app.event.onForeground(onForegroundCallback);
  
  blackberry.app.event.onBackground(null);
  blackberry.app.event.onExit(null);	

  blackberry.app.exit();

}	

function onForegroundCallback(){
  // checks to see if there are modified items to pull from server 
  // If yes then download and update sqlite database

  blackberry.app.event.onForeground(null);
}