Search code examples
androidback-buttonnativescript

nativescript - How to simulate the Back button on a mobile app?


Currently I am working on a mobile application using Nativescript. I would like to simulate the Back button by clicking a component in my UI (when user clicks it it navigates back to previous screen).

Scenario:
Page 1 --> Page 2
Page 2 : click on component to go back to Page 1

Is there a way to do this?

Thanks for your time.


Solution

  • in pageB.js

    var frameModule = require("ui/frame");
    var topmost = frameModule.topmost();
    
    function onTap() {
        topmost.goBack();
    }
    
    exports.onTap = onTap;
    

    in pageB.xml

    <Button text="BACK" tap="onTap" />
    

    More detailed information at the following documentation article:

    https://docs.nativescript.org/core-concepts/navigation#architecture-and-navigation