Skip to content
This repository was archived by the owner on Nov 17, 2022. It is now read-only.

Commit 2beb837

Browse files
authored
Merge pull request #1420 from NativeScript/niliev/activity
docs: Updated extended activity code
2 parents fd596cd + 86ea44b commit 2beb837

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

docs/core-concepts/android-runtime/advanced-topics/extend-application-activity.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ position: 2
66
---
77

88
# Extending Application and Activity
9-
This article describes how to create custom `android.app.Application` and `android.support.v7.app.AppCompatActivity` implementations in a NativeScript application. Demo code below is taken from the [Android Extend Sample](https://github.com/NativeScript/sample-android-extend).
9+
This article describes how to create custom `android.app.Application` and `android.support.v7.app.AppCompatActivity` implementations in a NativeScript application.
10+
11+
> **Note**: Demo code below is taken from the Android Extend demos for [plain JavaScript](https://github.com/NativeScript/nativescript-dev-webpack/blob/master/demo/JavaScriptApp/app/activity.android.js), [TypeScript](https://github.com/NativeScript/nativescript-dev-webpack/blob/master/demo/TypeScriptApp/app/activity.android.ts) or [Angular](https://github.com/NativeScript/nativescript-dev-webpack/blob/master/demo/AngularApp/app/activity.android.ts) applications.
1012
1113
## Philosophy
1214
Because NativeScript is a JavaScript-to-Native framework, our main goal is to make as much as possible from the underlying native platforms easy to implement in JavaScript. Initially we discussed the option where developers would write Java code to achieve some more special cases like custom `android.support.v7.app.AppCompatActivity` implementations but then we agreed that we should explore a JavaScript approach first and only if it is not possible to fallback to native code. It turned to be pretty easy, especially with the new [Static Binding Generator (SBG)](https://www.nativescript.org/blog/details/static-binding-generator---what-is-it-good-for) tool.
@@ -108,6 +110,9 @@ The core modules ship with a default `android.support.v7.app.AppCompatActivity`
108110
const superProto = android.support.v7.app.AppCompatActivity.prototype;
109111
android.support.v7.app.AppCompatActivity.extend("org.myApp.MainActivity", {
110112
onCreate: function(savedInstanceState) {
113+
// Set the isNativeScriptActivity in onCreate (as done in the original NativeScript activity code)
114+
// The JS constructor might not be called because the activity is created from Android.
115+
this.isNativeScriptActivity = true;
111116
if(!this._callbacks) {
112117
frame.setActivityCallbacks(this);
113118
}
@@ -144,9 +149,14 @@ The core modules ship with a default `android.support.v7.app.AppCompatActivity`
144149
145150
@JavaProxy("org.myApp.MainActivity")
146151
class Activity extends android.support.v7.app.AppCompatActivity {
152+
public isNativeScriptActivity;
153+
147154
private _callbacks: AndroidActivityCallbacks;
148155
149156
public onCreate(savedInstanceState: android.os.Bundle): void {
157+
// Set the isNativeScriptActivity in onCreate (as done in the original NativeScript activity code)
158+
// The JS constructor might not be called because the activity is created from Android.
159+
this.isNativeScriptActivity = true;
150160
if (!this._callbacks) {
151161
setActivityCallbacks(this);
152162
}

0 commit comments

Comments
 (0)