当 backstack 为空时,按下 android 上的后退按钮会破坏导航逻辑 [英] Pressing back button on android breaks navigation logic when backstack is empty

查看:60
本文介绍了当 backstack 为空时,按下 android 上的后退按钮会破坏导航逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have had similar issues with back button before. In my app I have a login page where the user submits their phone number and then they are taken to a page where they enter the verification code that hey have received. If the user pressed the back button on that page, the app gets minimized but when it's opened again the login page is shown instead of the page for submitting the verification code, even though I've specified clearhistory: true.

这就是我的导航方式:

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

推荐答案

您必须使用 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);
        }
    });
}

游乐场示例

这篇关于当 backstack 为空时,按下 android 上的后退按钮会破坏导航逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆