How to raise/notify suspend application event on android


#1

Hi,

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(http://fluentreports.com/blog/?p=261):

// only do this on android
if (application.android) {
    application.android.on(application.AndroidApplication.activityBackPressedEvent, 
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.


#2

@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.


#3

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.


#4

To suspend application on back button you can do something like below,

app.android.on(app.AndroidApplication.activityBackPressedEvent, (args: app.AndroidActivityBackPressedEventData) => {
        if (!frame.topmost().canGoBack()) {
            args.cancel = true;
            app.android.foregroundActivity.moveTaskToBack(true);
        }
    });

#5

@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 (application.android) {
    application.android.on(application.AndroidApplication.activityBackPressedEvent, function(args) {
        if (nextCancel) {
            args.cancel = true;
            // toast is a community plugin, see: https://www.npmjs.com/package/nativescript-toast
            Toast.makeText('Click again to exit.').show();

            if (nextCancelResetterHandle) {
                clearTimeout(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