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

Java Development

Reply
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.634 Dev OS, Z30 (STA100-5)-10.3.1.634 Dev OS, Passport (SQW100-1)-10.3.0.1154, PlayBook (16GB)-2.1.0.1917

OnExit listener

Is there a way to detect when an application exits. I come from a .Net background and have used "destructors" to detect when an application exits. Maybe I missed something but it doesn't seem like there are destructors in java so I can't detect when the app exits.

 

I understand you can just override the onClose() function but I am writing a library that creates temp files internally and want to delete the files on when the app exits.

 

Is there any way to do this?

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
New Developer
jlgosse
Posts: 41
Registered: ‎05-24-2009
My Device: Storm 9530

Re: OnExit listener

So wait. Why isn't onClose useful for this?
BlackBerry Mobile Software Developer
http://www.mobilitea.com/blog/
Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.634 Dev OS, Z30 (STA100-5)-10.3.1.634 Dev OS, Passport (SQW100-1)-10.3.0.1154, PlayBook (16GB)-2.1.0.1917

Re: OnExit listener

Because I am not doing this from a Screen, I have a library that does not create a Screen, then I need to detect when the app that is using the library closes so I can run any cleanup processes.
---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Developer
bneeman
Posts: 132
Registered: ‎02-11-2009
My Device: Not Specified

Re: OnExit listener


jlgosse wrote:
So wait. Why isn't onClose useful for this?

He's developing a library (I'm going to take a wild guess here and say there are no Screens in the library). With Screen.onClose, the responsibility to make the call that frees the temp files is shifted from the library developer to the app developer. If the app developer doesn't read the docs, the call isn't made and files are left stranded.

 

The design would be much nicer if the library itself could take care of that. But on BlackBerry, that is not possible. Destroctors in JAVA never really worked. Most BlackBerry apps terminate with System.exit() which is a very abrupt exit.

 

You will need to ask the app developer to call lib.cleanup() before calling System.exit().

 

One thing you can do from the lib, is cleanup when starting (assuming you can tell your own temp files from non-related temp files). That way, if the app developer neglects to make the call, at least the lib isn't building a pile of garbage on the drive.

 

Cheers, Barak.

 

Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.634 Dev OS, Z30 (STA100-5)-10.3.1.634 Dev OS, Passport (SQW100-1)-10.3.0.1154, PlayBook (16GB)-2.1.0.1917

Re: OnExit listener

Perfect, you took what I for whatever reason couldn't put into words and explained it.

 

As for your idea, I like the idea but worry that some files could be very large (I have an idea (not the current one) for a on device Video Editor and working with videos is a very, very, very, ... memory and hdd intensive so I wouldn't want to leave these temp files on a device) and could be required to be removed when the app is closed.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Developer
bneeman
Posts: 132
Registered: ‎02-11-2009
My Device: Not Specified

Re: OnExit listener

Leaving very large files around is actually better in this regard than leaving small files, because they are hard to miss. After a couple of runs in the simulator the app de will surely notice the space running out =)

 

Provide a static cleanup method, document it, and hand off responsibility to the app dev. Not the prettiest of designs, but it is the best you can do on the BlackBerry.

 

Cheers, Barak.

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: OnExit listener

Another strategy might be to have the calling application pass a reference to itself to the library on initialization.

 

The library might then create a watchdog thread that periodically checks Application.isAlive(), closing up shop and releasing resources when the last client is dead.

 

 

Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.634 Dev OS, Z30 (STA100-5)-10.3.1.634 Dev OS, Passport (SQW100-1)-10.3.0.1154, PlayBook (16GB)-2.1.0.1917

Re: OnExit listener

I thought about it last night and I think I'm going to do what bneeman said and have the app dev take care of exiting... for now. I won't say the problem is solved because I want this to be automated. What RexDoug said seems like a good idea I just hope that a thread with a loop to check the app does not get shut down by the VM before everything else. But for now, just so that I can finish this app I am working on I will call an temp Exit function.

 

Thanks for the ideas and help, will post anything if I figure out how to automate it.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
New Developer
drequena
Posts: 2
Registered: ‎06-01-2010
My Device: Curve 8900

Re: OnExit listener

[ Edited ]

Would the following variation of RexDoug's aproach work?

 

- Start the watchdog thread in the library's libmain() method.

- it maintiains a Hashtable of resources keyed by Application instances where each time a given lib service is requested the requestor Application gets added if not aready there .

- when some key.isAlive=false, do cleanup

 

I'm assuming said Hashtable would be a singleton in the RuntimeStore.

I'm also asuming Hashtable is threadsafe as well.

 

Admitedly this aproach (if doable at all) would incur in some nasty overhead but, come on, OP was talking about video processing!

 

Any inputs?

Please use plain text.
New Developer
New Developer
rzr
Posts: 151
Registered: ‎01-05-2010
My Device: dev alpha c (bb10)

Re: OnExit listener

I thought about overloading finalize function to do that Exit handle but i fear this happend too late ...

 

Are there some samples that "release a library" on exit ?

 

Please use plain text.