在 android 上按下后退按钮会在后台堆栈为空时中断导航逻辑

Pressing back button on android breaks navigation logic when backstack is empty

后退按钮 我也遇到过类似的问题。 在我的应用程序中,我有一个登录页面,用户可以在其中提交他们的 phone 号码,然后他们将被带到一个页面,在那里他们输入他们收到的验证码。如果用户按下该页面上的后退按钮,应用程序将最小化,但当它再次打开时,将显示登录页面而不是用于提交验证码的页面,即使我指定了 clearhistory: true.

我是这样导航的:

this.$navigateTo(ConfirmSMS, {
  transition: {name: 'slideLeft'},
  clearHistory: true
});

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

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

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

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);
        }
    });
}

Playground Sample