如何在NativeScript应用程序中双击后退按钮退出应用程序

如何在NativeScript应用程序中双击后退按钮退出应用程序,nativescript,Nativescript,在Nativescript中双击后退按钮退出应用程序 请帮助我了解代码片段以下是我找到的解决方案: var frameModule = require("ui/frame"); var application = require("application") var activity = application.android.startActivity || application.android.foregroundActivity || frameModu

在Nativescript中双击后退按钮退出应用程序


请帮助我了解代码片段

以下是我找到的解决方案:

var frameModule = require("ui/frame");
var application = require("application")

var activity = application.android.startActivity ||
        application.android.foregroundActivity ||
        frameModule.topmost().android.currentActivity ||
        frameModule.topmost().android.activity;
var lastPress;

activity.onBackPressed = function() {
    var timeDelay = 500
    if (lastPress + timeDelay > java.lang.System.currentTimeMillis()) {
        var startMain = new android.content.Intent(android.content.Intent.ACTION_MAIN);
        startMain.addCategory(android.content.Intent.CATEGORY_HOME);
        startMain.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.startActivity(startMain);

        // If you want to kill the app totally, use these codes instead of above
        // activity.finish();
        // java.lang.System.exit(0);

    } else {
        frameModule.topmost().goBack();
    }
    lastPress = java.lang.System.currentTimeMillis();
}

希望这能有所帮助。

以下是我找到的解决方案:

var frameModule = require("ui/frame");
var application = require("application")

var activity = application.android.startActivity ||
        application.android.foregroundActivity ||
        frameModule.topmost().android.currentActivity ||
        frameModule.topmost().android.activity;
var lastPress;

activity.onBackPressed = function() {
    var timeDelay = 500
    if (lastPress + timeDelay > java.lang.System.currentTimeMillis()) {
        var startMain = new android.content.Intent(android.content.Intent.ACTION_MAIN);
        startMain.addCategory(android.content.Intent.CATEGORY_HOME);
        startMain.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.startActivity(startMain);

        // If you want to kill the app totally, use these codes instead of above
        // activity.finish();
        // java.lang.System.exit(0);

    } else {
        frameModule.topmost().goBack();
    }
    lastPress = java.lang.System.currentTimeMillis();
}

希望这有帮助。

退出是否意味着关闭应用程序或完全杀死它?只关闭:)退出是否意味着关闭应用程序或完全杀死它?只关闭:)我应该把它包括在哪里?这可以放在任何地方,如何使用java.lang.system退出(0)并导入java。此外,我将{N}与angular一起使用。谢谢你的代码。有一件事,我怎样才能在第一次按back键时添加一个祝酒词,说“再次按back键退出”?@VeeK inside else block我在哪里包括这个?这可以放在任何地方,如何使用java.lang.system exit(0)和任何java导入。此外,我将{N}与angular一起使用。谢谢你的代码。只有一件事,我怎么能在第一次按下后加上祝酒词,说“再次按下后退出”?@VeeK inside else block