Update: (10/19/2016)
Since this post was initially written, there have emerged official ways to handle this. We provide documentation on creating launch screens with Android and also creating launch screens with iOS. A launch screen is shipped with the official NativeScript app template. On iOS apps, a splash screen is mandatory for App Store approval.
So, I asked the web (thanks, Google!) what our options are and it turned the most common approach is to display an additional Activity, showing the image, and then to proceed with the main Activity. This is a viable approach and my initial attempt was to utilize it, but I soon realized it is not appropriate in the context of a NativeScript application. Since we initialize the V8 engine and process all the JavaScript on the UI thread (bear in mind the overridden in JavaScript onCreate method of the main Activity), opening a new Activity would not donate to the required result.
After searching for other options, without any success, I decided to take a closer look to the drawing lifecycle and visual graph in Android. What caught my attention was the setBackgroundDrawableResource(int resId) method of the main Activity’s Window. So, instead of opening a new Activity to display an image, I decided to go after changing the Window’s background as appropriate. After some trial-error experiments, I finally came with a solution, which you may find useful for your applications.
<
resources
>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<
style
name
=
"AppBaseTheme"
parent
=
"android:Theme.Light"
>
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</
style
>
<!-- Application theme. -->
<
style
name
=
"SplashTheme"
parent
=
"AppBaseTheme"
>
<!-- Disable window features like ActionBar, Title and Overlay. -->
<
item
name
=
"android:windowActionBar"
>false</
item
>
<
item
name
=
"android:windowNoTitle"
>true</
item
>
<
item
name
=
"android:windowContentOverlay"
>@null</
item
>
<
item
name
=
"android:windowBackground"
>@drawable/splashscreen</
item
>
</
style
>
<
style
name
=
"AppTheme"
parent
=
"AppBaseTheme"
>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</
style
>
</
resources
>
<
activity
android:name
=
"com.tns.NativeScriptActivity"
android:label
=
"TestApp"
android:theme
=
"@style/SplashTheme"
>
var
application = require(
"application"
);
// check the current platform (we are interested in android only)
// alternatively, you may have app.android.js and app.ios.js
var
platform = require(
"platform"
);
if
(platform.device.os === platform.platformNames.android) {
application.onLaunch =
function
(intent) {
// hook the onActivityCreated callback upon application launching
application.android.onActivityCreated =
function
(activity) {
// apply the default theme once the Activity is created
var
id = activity.getResources().getIdentifier(
"AppTheme"
,
"style"
, activity.getPackageName());
activity.setTheme(id);
}
}
}
Well, pretty much that's it - happy splash-screening with NativeScript and Android :)