NativeScript equivalent to OnCreate


#1

Hello,

Is there a NativeScript equivalent hook for Android’s onCreate method for Angular Native on Android?


#2

You might be looking for this event: https://docs.nativescript.org/api-reference/classes/application.androidapplication.html#onactivitycreated

Which is part of the application module. Below is some code I have in my app.ts and have used to monitor events and setup fresco.

/// core
import application = require("application");
import { isAndroid, isIOS } from 'platform';

/// Android specifics
if (isAndroid) {

    /// Android onLaunch    
    application.onLaunch = ((intent) => {

        /// initialize Fresco library
        frescoInitialize();

        // /// initialize Google Analytics
        // googleAnalyticsInitialize({
        //     trackingId: 'UA-whatever-1',
        //     dispatchInterval: 45, /// (Value in seconds)...Default Android is 30 minutes, default iOS is 2 minutes (120 seconds).  Disable by setting to 0.
        // })

    })

    /// Activity Started    
    application.android.on('activityStarted', ((args: application.AndroidActivityEventData) => {
        console.log('*** Activity Started ***');
    }))

    /// Activity Paused    
    application.android.on('activityPaused', ((args: application.AndroidActivityEventData) => {
        console.log('*** Activity Paused ***');
    }))

    /// Activity Resumed
    application.android.on('activityResumed', ((args: application.AndroidActivityEventData) => {
        console.log('*** Activity Resumed ***');
    }))

    /// Activity Stopped    
    application.android.on('activityStopped', ((args: application.AndroidActivityEventData) => {
        console.log('*** Activity Stopped ***');
    }))

    /// Activity Destroyed    
    application.android.on('activityDestroyed', ((args: application.AndroidActivityEventData) => {
        console.log('*** Activity Destroyed ***');
        // googleAnalyticsDispatch(); /// this will log anything left when the activity is destroyed if it's in the queue
    }))

}

#3

Hi Brad,

I specifically needed to hook on android on create. I have managed to do this with the help of this NativeScript article https://docs.nativescript.org/runtimes/android/advanced-topics/extend-application-activity#extending-activity. Haven’t read anywhere if onlaunch or activityCreated are equivalent to onCreate yet.