yukashima huksay
yukashima huksay

Reputation: 6238

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

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 is how I navigate:

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

Upvotes: 1

Views: 639

Answers (1)

Manoj
Manoj

Reputation: 21908

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

Upvotes: 4

Related Questions