A common scenario in android development is being able to add a custom activity and navigate to it.
There are also several very popular libraries that requires this functionality. See for example the native YouTube SDKs. Here is a snippet from their documentation:
A view for displaying YouTube videos. Using this View directly is an alternative to using the
If you choose to use this view directly, your activity needs to extend
The way you add a custom Activity or a class in native android application, is to declare it in the AndroidManifest.xml file, as well as to create the declared class. The same goes not just for activities, but for any application component.
Thanks to the SBG, now you can have custom Java classes and all application components, declared in the AndroidManifest.xml file. That enables the full native functionality already present in android. To make things clearer, let’s give a common example
Step1 - declare the Activity in the AndroidManifest.xml file
As we saw the Activity is an android class that needs to be declared in the AndroidManifest.xml, so let’s do that first.
Go to app/App_Resources/Android/AndroidManifest.xml and add an activity tag, so the xml file look something like this:
NOTE: Notice the “MyActivity” class we just declared needs to be put in at least one namespace, or else it won’t be recognized as a custom class.
Step2: Provide the actual class implementation
Now that we have declared the custom activity in the manifest we need to provide the actual class implementation.
Create that custom js class that will contain the logic in the declared activity: app/MyActivity.js.
A minimal implementation of this file looks like:
TypeScript file: "app/MyActivity.ts"
NOTE: Notice that the activity we extend is a native activity - android.app.Activity. Here is the place to choose what native class we want to extend. We could also inherit android.support.v7.app.AppCompatActivity with the same result, and that’s a great feature we didn’t have until 2.0.
NOTE: The name of the extend and the activity name, declared in the AndroidManifest.xml, need to be the same: com.tns.MyActivity.
As mentioned some common application components that take advantage of the SBG are activities, applications and widgets. Here are some examples with common scenarios that take advantage of the SBG:
The second example is an implemented android widget.
NOTE: Both examples follow the recommended android way of enabling widgets and multidex support.
There is a bonus that comes with the SBG, performance-wise. There is a significant boost on the initial loading time of the application. Here are the numbers for the default NativeScript app, ran on Nexus 5.
Project structure after build:
AndroidManifest.xml file you can edit: add activities, application and other application components
The build will take care of compiling the generated java file com/tns/MyActivity.java (3), and packing it into the application.
Now anyone keen to implement a YouTube player plugin for NativeScript? :)