Nativescript: Navigation does not work if go home and click again in app icon in Adnroid

Created on 31 Oct 2018  路  5Comments  路  Source: NativeScript/NativeScript

Hi,

I have a page in app-root that detect if user is loggued in and redirect to login or to main page.
If I press the home button and tap again in the app icon, the navigation property does not work any more.
What can I do?

The page have this code:

function onPageLoaded(args) {
    //const page = args.object;

    dataManager.GetUserData(function (userData) {
        if (userData == null)
            global.Navigate("views/login");
        else
            global.Navigate("views/tab-menu");
    });
}
exports.onPageLoaded = onPageLoaded;

And app.js is:

var application = require("application");
const topmost = require("tns-core-modules/ui/frame").topmost;
const dataManager = require("~/shared/dataManager.js").DataManager;
const wsAPI = require("~/shared/wsAutocorp.js").wsAPI;
const getFrameById = require("tns-core-modules/ui/frame").getFrameById;

// application.on(application.launchEvent, (args) => {
//     //const frame = getFrameById("frmRoot");

//     if (args.android) {
//         // For Android applications, args.android is an android.content.Intent class.
//         console.log("Launched Android application with the following intent: " + args.android + ".");
//     } else if (args.ios !== undefined) {
//         // For iOS applications, args.ios is NSDictionary (launchOptions).
//         console.log("Launched iOS application with options: " + args.ios);
//     }


//     // dataManager.CreateOrOpenDatabase(function () {
//     //     console.log("DB lista!");
//     // });
// });

global.GetFrame = function () {
    const frame = getFrameById("frmRoot");
    return frame;
}

global.Navigate = function (page) {
    global.GetFrame().navigate(page);
}

global.NavigateNoBack = function (page) {
    const navigationEntry = {
        moduleName: page,
        // Page navigation, without saving navigation history.
        backstackVisible: false,
        clearHistory: true
    };
    global.GetFrame().navigate(navigationEntry);
}

global.GoBack = function () {
    topmost().goBack();
}

global.ShowModal = function (page) {
    global.GetFrame().showModal(page);
}

global.Alert = function (title, message, okButtonText, callbackFunction) {
    const alertOptions = {
        title: title,
        message: message,
        okButtonText: okButtonText,
        cancelable: false // [Android only] Gets or sets if the dialog can be canceled by taping outside of the dialog.
    };

    alert(alertOptions).then(() => {
        if (callbackFunction != null)
            callbackFunction();
    });
}

application.run({
    moduleName: "app-root"
});
needs more info android

All 5 comments

Hi, @neumartin,
Please provide a sample project, which can be used for debugging and demonstrates what you are trying to achieve as a final result.

Thanks @tsonevn !

See this project http://www.neumartin.com.ar/Files/SampleErrorNS.zip
First time works perfect, but if you press home and tap in the app icon again, the navigation dos not work.
Thanks!

HI @neumartin,
I would suggest using navigatemethod provided by the frame module, directly in the loading-page.js file instead of using the global functions which you have defined in the app.js. Also, make sure that you are calling the navigate method after the page is fully loaded. Regarding that, I would suggest making the navigation on navigatedTo event. For example:
loading-page.xml

<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="onPageLoaded" navigatedTo="onNavigatedTo" class="page">
    <Page.actionBar>
        <ActionBar title="My App" icon="" class="action-bar">
        </ActionBar>
    </Page.actionBar>
    <StackLayout class="p-20">
        <Label text="Loading..." class="h1 text-center"/>
    </StackLayout>
</Page>

loading-page.js

const frameModule = require("tns-core-modules/ui/frame");
function onNavigatedTo(args) {
    frameModule.topmost().navigate("main-page");
}
exports.onNavigatedTo = onNavigatedTo;

Also, what is the reason for defining such those global functions?

Works!, thanks!!
I used this functions in global becouse I want to have centralized the basic functionality, but I see is better your way.
Thanks!

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings