1

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
});
yukashima huksay
  • 5,834
  • 7
  • 45
  • 78
  • If you use `clearHistory: true` then the history stack will be cleared, hitting back button will not minimize the app, instead will terminate it. So when you open it again, you will get login screen again, it's expected behavior. – Manoj Apr 24 '20 at 06:03
  • @Manoj what do you mean it will terminate it? I see the app in the list of open applications after pressing the back button. – yukashima huksay Apr 24 '20 at 06:07
  • @Manoj in any case, how can I make the back button minimize the app instead of terminating it when the stack is empty? Is there any workaround for this issue? – yukashima huksay Apr 24 '20 at 06:08
  • You will have to listen to back button event and if there are no items on stack, then cancel the action and start a new intent that will aim for home screen. – Manoj Apr 24 '20 at 06:10
  • @Manoj Thank you, I'm using [this](http://fluentreports.com/blog/?p=261) for the listener, how do I check if navigation stack is empty in nativescript? – yukashima huksay Apr 24 '20 at 06:39
  • Refer the [docs](https://docs.nativescript.org/api-reference/classes/_ui_frame_.frame#cangoback), you may use **canGoBack()** method on frame. – Manoj Apr 24 '20 at 06:41
  • @Manoj thank you so much, and how do I get a reference to frame without using $refs? – yukashima huksay Apr 24 '20 at 06:50
  • If you refer the same documentation, you can get the topmost frame or find frame by id if it's multi frame application. Page will also have reference to frame always. – Manoj Apr 24 '20 at 15:31

1 Answers1

4

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

Manoj
  • 21,753
  • 3
  • 20
  • 41
  • The reason I came across this problem was that I found some of my less tech savvy users pressing the back button in order to go read the verification code from they sms evnetho they could already read it from it's notification. – yukashima huksay Apr 25 '20 at 15:15
  • This works only if app is reopened from the recent apps. but not when app is reopened by clicking on the app icon. is there no way to fix that? I know this is also the case when pressing hope button, I'm not asking a question because I don't want to have another question for which you just will comment that it is impossible:D If it's possible tell me to ask a new question :D – yukashima huksay Apr 28 '20 at 16:32
  • At this point, tapping on app icon will also bring the app back (paused -> resume). This will not restart the app unless it was killed from recent app / you have some special manifest settings. – Manoj Apr 28 '20 at 19:38
  • Where is android imported from? my linter naggs that it is not defined eventho the code works just fine – yukashima huksay May 07 '20 at 06:41
  • That's native name space, use declare syntax or install tns platform declarations if you are using TypeScript. Or you may even disable linter for those lines. – Manoj May 07 '20 at 07:15
  • I'm not using TS. what is declare syntax? – yukashima huksay May 07 '20 at 08:51
  • I don't use JS much these days. Declare is also for TS. You should just disable linter on the specific lines. – Manoj May 07 '20 at 09:18
  • 1
    In case someone using eslint gets here later. see [this](https://eslint.org/docs/user-guide/configuring#specifying-globals) – yukashima huksay May 08 '20 at 06:33