且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当 backstack 为空时,按下 android 上的后退按钮会破坏导航逻辑

更新时间:2023-01-10 12:21:16

您必须使用 clearHistory 仅当您不想使用按后退按钮返回登录时.

You must use clearHistory only if you don't want use to go back to Login back upon pressing back button.

当您按下返回按钮并且返回堆栈中没有页面时,应用程序将终止.它仍然会出现在最近的应用程序中,但与 iOS 不同的是,点击最近的应用程序会重新启动它,除非它被暂停但是另一个活动/主页按钮.

When you press back button and there are no Pages in back stack, application will terminate. It will still appear in recent application but unlike iOS tapping on the recent application will restart it unless it was paused but another activity / home button.

您可以覆盖后退按钮来暂停应用程序而不是终止它.

You may override back button to pause application instead of terminating it.

import { isAndroid } from "@nativescript/core/platform";
import * as application from "@nativescript/core/application";
import { Frame } from "@nativescript/core/ui/frame";

if (isAndroid) {
    application.android.on(application.AndroidApplication.activityBackPressedEvent, function (args) {
        const frame = Frame.topmost();
        if (frame && !frame.canGoBack()) {
            args.cancel = true;
            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);
            application.android.foregroundActivity.startActivity(startMain);
        }
    });
}

游乐场示例