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

后端 未结 1 1785
长发绾君心
长发绾君心 2021-01-28 01:40

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

1条回答
  •  粉色の甜心
    2021-01-28 02:11

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

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

    Playground Sample

    0 讨论(0)
提交回复
热议问题