Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

As a WebWorks developer, I would like to listen for an "onexit" or "onclose" event. #191

Open
RonMen opened this Issue · 10 comments

5 participants

@RonMen

Since a WebWorks developer can programmatically close an app with the blackberry.app.exit() call would it be possible to listen for when the app get closed to do something before closing the app?

The blackberry.app.exit() can be handled from an apps menu to do something before exiting the app, but if the user minimizes and closes the app, IMHO, we should also be able to do something "onbeforeexit" to eg. don't lose data.

@nukulb

the problem with this event is reliability, you will get the event no problem, in fact you can easily do it but its not a sync API which means that your webview will already be closed before you can really do something useful.
I can tell you how to hook up the event if you want to try it yourself.

@RonMen

Would be nice if you can point me to something to give it a try. I think it is essential to have something like onbeforeexit to be able to handle things.

@nukulb

In your downloaded build go to /Framework/lib/framework.js

In the registerApplicationEvents add an event listener for onExit

function registerApplicationEvents() {
    if (window.chrome && window.chrome.internal) {
        chrome.internal.application = {};
        chrome.internal.application.onWindowState = function (state) {};
        chrome.internal.application.onSwipeDown = function () {
            if (onSwipeDown) {
                onSwipeDown();
            }            
        };
        chrome.internal.application.onWindowActive = function () {
            if (onResume) {
                onResume();
            }            
        };
        chrome.internal.application.onWindowInactive = function () {
            if (onPause) {
                onPause();
            }            
        };
        //just added
        chrome.internal.application.onExit = function () {
            if (onExit) {
                onExit();
            }            
        };
    }
}

Add an onExit var /Framework/lib/framework.js and also add add a setter

 onSwipeDown = null,
    onPause = null,
    onResume = null,
    onExit = null;

setOnExit: function (handler) {
        onExit = handler;
}

Go to / Framework / ext / blackberry.app / navEvents.js

In the addEventListener
add another case for the onExit

 case "onExit":
                framework.setOnExit(trigger);
                break;

In the / Framework / ext / blackberry.app / index.js

In the _actionMap add another item for exit
 exit: {
            context: require("./navEvents"),
            event: "exit",
            trigger: function () {
                _event.trigger("exit");
            }
        }

now you should be to call blackberry.addEventListener("exit",...) from your client code.

@RonMen

THX for adding source here. Added it line by line to my SDK but it doesn't work for me. The only one thing I changed is when registering the event handler, I used blackberry.event.addEventListener("exit", function(evt) {console.log(evt);}, false); instead of yours blackberry.addEventListener("exit",...) without the event in there.

BTW: I revisited my .bar file and all the things done are in there!

@nukulb

yes yours is correct and you don't need the third parameter. So its suppose to work like this -
blackberry.event.addEventListener("exit", function () {});
You can put a console.log here and see whether it gets called on the framework or not?

    chrome.internal.application.onExit = function () {
        console.log("exit");
        if (onExit) {
            onExit();
        }            
    };

Now carefully have webinspector open and see whether you get the console.log or not.

@kwallis
Owner

Hey guys, where do we stand with this?

@RonMen

Hi. @nukulb mentioned that the webview of an app already will be closed before something happend this isn't a good usecase for an onexit event handler/listener, isn't it? All that I wanted to be aware of is to make sure that the state of the data used in an app can be stored to LocalStorage/DB before the app gets closed. From inside I'm able to handle it with the blackberry.app.exit() but this happens before the webview is really closed. If the user minimizes app to background and swipes it to close nothing can be done here and that can't be a solution I think.

@nukulb
@ctetreault

Any update on this?

@bryanhiggins
Collaborator

I looked into this a few months ago, but I could not get an event into the client web view before it got terminated.

The conclusion was that this is not possible without making changes on device.

#393

blackberry-webworks@fb591e7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.