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: 587
Registered: ‎04-01-2009
My Device: Z10, PlayBook
My Carrier: NA

Saving on Exit

When the user exit's my application via the "Exit" button, I save their data to a SharedObject, but if they swipe up from the bottom bezel and then exit the application via the "X" button (etc), their data is lost.

 

I'd prefer not to save their data every time they click on a button in the data entry screen since that could mean saving a few KB of data every second or so, which seems crude, not battery friendly, etc.

New Developer
Posts: 18
Registered: ‎11-14-2010
My Device: Not Specified

Re: Saving on Exit

There a re alot of things we can't do yet becausewe are still still limited to flash.  I would really like to have threads.

Highlighted
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Saving on Exit

hey dbigham,

 

you can add an event listener to see when your application goes into the background and/or is minimized using the following line of code:

 

 

NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, saveMyData);

 

then you can call your function saveMyData and save the data in the event that the user does exit the application. hope that helps! 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: 409
Registered: ‎12-10-2010
My Device: PlayBook
My Carrier: N/A

Re: Saving on Exit

@jjackson:  we can't author threads (for the time being) in Flash, but AIR isn't completely single threaded.  of interest, the flash.fileSystem package has several asynchronous methods which the OP could alternatively use to save data.


PlayBook Applications:
Drop Swatch
Developer
Posts: 587
Registered: ‎04-01-2009
My Device: Z10, PlayBook
My Carrier: NA
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Saving on Exit

 


TheDarkIn1978 wrote:

@jjackson:  we can't author threads (for the time being) in Flash, but AIR isn't completely single threaded.  of interest, the flash.fileSystem package has several asynchronous methods which the OP could alternatively use to save data.


 

That would avoid impacting user responsiveness in his app, but I think he's right about it being "crude" and not so battery-friendly etc, compared to using the DEACTIVATE event, for his particular requirement.

 

By the way, AIR actually is single-threaded, but our apps are "event-driven", which means they execute code only in response to a new event which the framework passes to them.  After handling it, they return to the framework (rather than sit in an endless loop), and it's the framework which handles the asynchronous stuff "outside" of our own code in this time between events.  That's why writing an infinite loop locks things up, and writing a long-running routine or one which blocks on some synchronous operation will destroy the app's responsiveness.

 

The net effect is similar to being multithreaded, in some ways more limited but in some ways better (avoiding various race conditions etc).  In complex apps I think it should help a developer to have an accurate picture of how this works.

 

(The Java and native SDKs should certainly allow multithreaded apps on the PlayBook, not to mention multi-processor ones.  Yay! :-) )


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: 73
Registered: ‎11-16-2010
My Device: Playbook

Re: Saving on Exit

[ Edited ]

Just expanding on the

 

 

NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, doStuffHere);

 

 

For the playbook it'd be:

 

 

QNXApplication.qnxApplication.addEventListener(Event.DEACTIVATE, doStuffHere).

 

As well it seems adobe AIR for desktops has Event.EXITING, which could be very helpful if it was implemented in the Blackberry SDK.

 

Cheers!

----------------------------
Let's turn things up...
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Saving on Exit

I'm not clear on the whole purpose of QNXApplication and NativeApplication, but for me the following in my Sprite subclass was sufficient to get notified when I was minimized or reactivated:

 

 

addEventListener(Event.DEACTIVATE, doStuffHere);

 

 Also, while I've confirmed that you can catch Event.EXITING on the nativeApplication object, it appears you can't prevent the app from exiting by calling e.preventDefault() on it.  It's also not clear what you could do as a result of catching that... possibly no asynchronous operations.  Deserves some experimentation.

 

Still, it does mean you can get the following to help manage the application life cycle:

 

  1. InvokeEvent.INVOKE on the nativeApplication when you are first launched
  2. Event.ACTIVATE on your main Sprite when your window opens or is reactivated
  3. Event.DEACTIVATE on your main Sprite when your window is minimized or the user switches apps
  4. Event.EXITING on the nativeApplication when the user finally closes your app for good

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: 137
Registered: ‎12-22-2010
My Device: Curve 8520
My Carrier: Software Developer

Re: Saving on Exit

Did anybody find a way to prevent an application to Exit? I want to implement a classical "Do you want to save the current file?" (Yes,No,Cancel)

 

On Desktop the EXITING-Event  and event.preventDefault() works. But on PB this Event isn't implemented Smiley Sad

 

regards

 

Levio

Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Saving on Exit

I'd strongly recommend that you modify your mental model of how the user should work in your app, and always save the current state upon DEACTIVATE.  That way if the app is terminated, either because of user action, a crash, or the OS killing things in low-memory conditions, you've persisted all the critical data already.

 

If you really think the user has to explicitly choose to overwrite their previous data with the modified data, then implement it so that the work-in-progress is saved to a temporary location.  Upon restarting the application (assuming it was exited), you can offer to the user to restore the previous state, or revert to the unsaved state.

 

It's possible the next release of the SDK will have some more explicit support for what you're trying to do, but I'd suggest you consider not using that even if it's present.  Given how often users are likely to leave apps open and swipe left/right between them, I expect you'll find users complaining that they don't like having to remember to save their data, but obviously I don't know your situation so free free to reject that thought.

 

If it's not possible to make such a change, however, I think your only option at the moment is to provide a clear "Exit" or "Done" button of some kind and hope/expect/train the user to click it to exit your app, rather than using the bezel swiping.  Then you can do the intrusive-popup thingy.


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!