I’ve been exploring NativeScript recently, and decided to go “back to the basics” to make sure I understood the foundation of NativeScript before using it on an upcoming production project. When going “back to the basics,” I mean reading the documentation provided by the Telerik team on the NativeScript website, and creating sample apps based upon the online documentation. When I was writing my demo app, I found that the NativeScript application launch event does not fire.
NativeScript Application Launch Event Does Not Fire
NativeScript apps have a core set of application-level events that fire throughout the app lifecycle (launch, suspend, resume, exit, low memory, and uncaught error). The launch event is supposed to be raised when the application launches; however, in my demo app, I could not get the lunch event to fire.
In my testing, I tried to simplify my code, and I had the following in my app.js file (which is the file acting as the application bootstrapper, launching the app):
var application = require("application"); application.mainModule = "views/main/main"; application.start(); // launch event raised when the application launches application.on(application.launchEvent, function (args) { console.log("App launching..."); });
When using the above code, the application launch event does not fire. When I originally wrote this code, it was probably too late at night, so I wasn’t really thinking what I wrote. Let’s break it down to see where my error was.
var application = require("application");
Line 1: load the “application” module, storing a reference to the module in the application variable.
application.mainModule = "views/main/main";
Line 2: set the main starting page to the view named “main”, located in “views/main”.
application.start();
Line 3: start the application, loading the main view.
// launch event raised when the application launches application.on(application.launchEvent, function (args) { console.log("App launching..."); });
Lines 5-8: tell the NativeScript runtime to print “App launching…” to the console when the application launches.
Launch Event Does Not Fire When Declared After the Application Starts
When I ran the above code, I couldn’t get my application to output to the console when it started. After several hours staring at my code in the middle of the night, I decided to sleep on it. When I woke up the next morning, I knew what I did wrong.
Did you see my mistake…it took me a restless night of sleep to realize the simple problem. One line 3, I started the application. After I started the application (lines 5-8), I told the NativeScript runtime to execute the anonymous function that outputs “App launching…” to the console. Let me say that again…After I start the application, I told the NativeScript runtime to execute a startup/launch command.
Launch Event Must Be Placed Before Application Start Command
To get my application to output the “App launching…” text to the console, all I had to do was tell my application to respond tot he launch event before the application started. Sounds simple, right? It is.
Here’s the updated code that I used in my app (after a good night’s sleep):
var application = require("application"); application.mainModule = "views/main/main"; // launch event raised when the application launches // if this command is not placed before the application.start(), then it won't execute application.on(application.launchEvent, function (args) { console.log("App launching..."); }); // start the application AFTER you tell it to respond to the launch event application.start();
Programming 101: Tell Your Application to do Something Before it is Supposed to do “It”
So, the lesson learned is something I learned in my first computer science class: if you want your application to do something, you need to tell it do do it before it is supposed to happen. After thinking how I got caught up on something this simple, I thought back to a great lesson I learned with my 2 young boys – you need to set the expectation first; otherwise, they don’t/won’t do what you’re expecting.
If you ensure your application launch event registration code is executed before the application starts, your launch event code will executes when the application starts.
Thank you for sharing.