How to raise/notify suspend application event on android



What I want is watch the Android’s activityBackPressedEvent, and raise the cross-platform suspend event, instead of directly exit the application.

I have following code according to the blog(

// only do this on android
if ( {, 
function(args) {
        console.log('AndroidApplication.activityBackPressedEvent happened');
        args.cancel = true;

        // application.notify(new application.ApplicationEventData({
            // eventName: application.suspendEvent,
            // android: ?,
            // object: args.object
        // }));

I don’t know how to construct this staff, as NativeScript doc didn’t say anything about this.


@xingyuli Back button is only there in Android. All application level events are documented here.

I would not suggest to fire suspend event yourself, it is pre-defined by framework and fired on application suspend. Rather you can name your own event, but as the backbutton is android specific I don’t really get why you have to fire an another event here.


I want this, as the back button is pressed on the home-page of my application, and the default behavior of Android simply exit the application if the page is the last frame in the stack. I don’t want the default behavior. And I know it is Android specific and that’s why I only do this on Android device. Users are not expecting exiting application as plenty applications just dismiss(suspend) itself in the background, which is what I want.


To suspend application on back button you can do something like below,, (args: app.AndroidActivityBackPressedEventData) => {
        if (!frame.topmost().canGoBack()) {
            args.cancel = true;


@manojdcoder Test against your neat solution, it works!

As some of the applications use another approach: prompt(toast) user to double click back-button for exiting. I also tried that, and here is the working code snippet for others’ reference:

let nextCancel = true;
let nextCancelResetterHandle;

// only do this on android
if ( {, function(args) {
        if (nextCancel) {
            args.cancel = true;
            // toast is a community plugin, see:
            Toast.makeText('Click again to exit.').show();

            if (nextCancelResetterHandle) {

            // enable exit if back button is tapped within 1 sec
            nextCancel = false;
            nextCancelResetterHandle = setTimeout(function() {
                nextCancel = true;
            }, 1000);

Big thanks!

Code-behind from one page works on other pages too