mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
Merge pull request #2271 from NativeScript/atanasovg/remove-activity-extend
Extract the Activity implementation logic in a separate class.
This commit is contained in:
41
tns-core-modules/ui/frame/activity.android.ts
Normal file
41
tns-core-modules/ui/frame/activity.android.ts
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
import {activityCallbacks as callbacks} from "ui/frame";
|
||||||
|
|
||||||
|
@JavaProxy("com.tns.NativeScriptActivity")
|
||||||
|
class NativeScriptActivity extends android.app.Activity {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
return global.__native(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected onCreate(savedInstanceState: android.os.Bundle): void {
|
||||||
|
callbacks.onCreate(this, savedInstanceState, super.onCreate);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected onSaveInstanceState(outState: android.os.Bundle): void {
|
||||||
|
callbacks.onSaveInstanceState(this, outState, super.onSaveInstanceState);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected onStart(): void {
|
||||||
|
callbacks.onStart(this, super.onStart);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected onStop(): void {
|
||||||
|
callbacks.onStop(this, super.onStop);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected onDestroy(): void {
|
||||||
|
callbacks.onDestroy(this, super.onDestroy);
|
||||||
|
}
|
||||||
|
|
||||||
|
public onBackPressed(): void {
|
||||||
|
callbacks.onBackPressed(this, super.onBackPressed);
|
||||||
|
}
|
||||||
|
|
||||||
|
public onRequestPermissionsResult (requestCode: number, permissions: Array<String>, grantResults: Array<number>): void {
|
||||||
|
callbacks.onRequestPermissionsResult(this, requestCode, permissions, grantResults, undefined /*TODO: Enable if needed*/);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected onActivityResult(requestCode: number, resultCode: number, data: android.content.Intent): void {
|
||||||
|
callbacks.onActivityResult(this, requestCode, resultCode, data, super.onActivityResult);
|
||||||
|
}
|
||||||
|
}
|
@ -400,8 +400,8 @@ export class Frame extends frameCommon.Frame {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var framesCounter = 0;
|
let framesCounter = 0;
|
||||||
var framesCache: Array<WeakRef<AndroidFrame>> = new Array<WeakRef<AndroidFrame>>();
|
let framesCache: Array<WeakRef<AndroidFrame>> = new Array<WeakRef<AndroidFrame>>();
|
||||||
|
|
||||||
class AndroidFrame extends Observable implements definition.AndroidFrame {
|
class AndroidFrame extends Observable implements definition.AndroidFrame {
|
||||||
public rootViewGroup: android.view.ViewGroup;
|
public rootViewGroup: android.view.ViewGroup;
|
||||||
@ -600,7 +600,7 @@ function getFrameById(frameId: number): Frame {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var animationFixed;
|
let animationFixed;
|
||||||
function ensureAnimationFixed() {
|
function ensureAnimationFixed() {
|
||||||
if (!animationFixed) {
|
if (!animationFixed) {
|
||||||
// android.os.Build.VERSION.KITKAT but we don't have definition for it
|
// android.os.Build.VERSION.KITKAT but we don't have definition for it
|
||||||
@ -724,31 +724,25 @@ class FragmentClass extends android.app.Fragment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@JavaProxy("com.tns.NativeScriptActivity")
|
class ActivityCallbacksImplementation implements definition.AndroidActivityCallbacks {
|
||||||
class NativeScriptActivity extends android.app.Activity {
|
private _rootView: View;
|
||||||
private rootView: View;
|
|
||||||
|
|
||||||
constructor() {
|
public onCreate(activity: android.app.Activity, savedInstanceState: android.os.Bundle, superFunc: Function): void {
|
||||||
super();
|
|
||||||
return global.__native(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected onCreate(savedInstanceState: android.os.Bundle): void {
|
|
||||||
if (trace.enabled) {
|
if (trace.enabled) {
|
||||||
trace.write(`NativeScriptActivity.onCreate(${savedInstanceState})`, trace.categories.NativeLifecycle);
|
trace.write(`Activity.onCreate(${savedInstanceState})`, trace.categories.NativeLifecycle);
|
||||||
}
|
}
|
||||||
|
|
||||||
let app = application.android;
|
let app = application.android;
|
||||||
let intent = this.getIntent();
|
let intent = activity.getIntent();
|
||||||
if (application.onLaunch) {
|
if (application.onLaunch) {
|
||||||
application.onLaunch(intent);
|
application.onLaunch(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
let args: application.LaunchEventData = { eventName: application.launchEvent, object: app, android: intent };
|
let launchArgs: application.LaunchEventData = { eventName: application.launchEvent, object: app, android: intent };
|
||||||
application.notify(args);
|
application.notify(launchArgs);
|
||||||
|
|
||||||
let frameId = -1;
|
let frameId = -1;
|
||||||
let rootView = args.root;
|
let rootView = launchArgs.root;
|
||||||
let extras = intent.getExtras();
|
let extras = intent.getExtras();
|
||||||
|
|
||||||
// We have extras when we call - new Frame().navigate();
|
// We have extras when we call - new Frame().navigate();
|
||||||
@ -790,14 +784,14 @@ class NativeScriptActivity extends android.app.Activity {
|
|||||||
// If there is savedInstanceState and activityInitialized is false we are restarted but process was killed.
|
// If there is savedInstanceState and activityInitialized is false we are restarted but process was killed.
|
||||||
// For now we treat it like first run (e.g. we are not passing savedInstanceState so no fragments are being restored).
|
// For now we treat it like first run (e.g. we are not passing savedInstanceState so no fragments are being restored).
|
||||||
// When we add support for application save/load state - revise this logic.
|
// When we add support for application save/load state - revise this logic.
|
||||||
var isRestart = !!savedInstanceState && activityInitialized;
|
let isRestart = !!savedInstanceState && activityInitialized;
|
||||||
super.onCreate(isRestart ? savedInstanceState : null);
|
superFunc.call(activity, isRestart ? savedInstanceState : null);
|
||||||
|
|
||||||
this.rootView = rootView;
|
this._rootView = rootView;
|
||||||
|
|
||||||
// Initialize native visual tree;
|
// Initialize native visual tree;
|
||||||
rootView._onAttached(this);
|
rootView._onAttached(activity);
|
||||||
this.setContentView(rootView._nativeView, new org.nativescript.widgets.CommonLayoutParams());
|
activity.setContentView(rootView._nativeView, new org.nativescript.widgets.CommonLayoutParams());
|
||||||
// frameId is negative w
|
// frameId is negative w
|
||||||
if (frame) {
|
if (frame) {
|
||||||
frame.navigate(navParam);
|
frame.navigate(navParam);
|
||||||
@ -806,49 +800,52 @@ class NativeScriptActivity extends android.app.Activity {
|
|||||||
activityInitialized = true;
|
activityInitialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected onSaveInstanceState(outState: android.os.Bundle): void {
|
public onSaveInstanceState(activity: android.app.Activity, outState: android.os.Bundle, superFunc: Function): void {
|
||||||
super.onSaveInstanceState(outState);
|
superFunc.call(activity, outState);
|
||||||
let view = this.rootView;
|
let view = this._rootView;
|
||||||
if (view instanceof Frame) {
|
if (view instanceof Frame) {
|
||||||
outState.putInt(INTENT_EXTRA, view.android.frameId);
|
outState.putInt(INTENT_EXTRA, view.android.frameId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected onStart(): void {
|
public onStart(activity: any, superFunc: Function): void {
|
||||||
super.onStart();
|
superFunc.call(activity);
|
||||||
|
|
||||||
if (trace.enabled) {
|
if (trace.enabled) {
|
||||||
trace.write("NativeScriptActivity.onStart();", trace.categories.NativeLifecycle);
|
trace.write("NativeScriptActivity.onStart();", trace.categories.NativeLifecycle);
|
||||||
}
|
}
|
||||||
let rootView = this.rootView
|
let rootView = this._rootView;
|
||||||
if (rootView && !rootView.isLoaded) {
|
if (rootView && !rootView.isLoaded) {
|
||||||
rootView.onLoaded();
|
rootView.onLoaded();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected onStop(): void {
|
public onStop(activity: any, superFunc: Function): void {
|
||||||
super.onStop();
|
superFunc.call(activity);
|
||||||
|
|
||||||
if (trace.enabled) {
|
if (trace.enabled) {
|
||||||
trace.write("NativeScriptActivity.onStop();", trace.categories.NativeLifecycle);
|
trace.write("NativeScriptActivity.onStop();", trace.categories.NativeLifecycle);
|
||||||
}
|
}
|
||||||
let rootView = this.rootView
|
let rootView = this._rootView;
|
||||||
if (rootView && rootView.isLoaded) {
|
if (rootView && rootView.isLoaded) {
|
||||||
rootView.onUnloaded();
|
rootView.onUnloaded();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected onDestroy(): void {
|
public onDestroy(activity: any, superFunc: Function): void {
|
||||||
let rootView = this.rootView
|
let rootView = this._rootView
|
||||||
if (rootView && rootView._context) {
|
if (rootView && rootView._context) {
|
||||||
rootView._onDetached(true);
|
rootView._onDetached(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
super.onDestroy();
|
superFunc.call(activity);
|
||||||
|
|
||||||
if (trace.enabled) {
|
if (trace.enabled) {
|
||||||
trace.write("NativeScriptActivity.onDestroy();", trace.categories.NativeLifecycle);
|
trace.write("NativeScriptActivity.onDestroy();", trace.categories.NativeLifecycle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public onBackPressed(): void {
|
public onBackPressed(activity: any, superFunc: Function): void {
|
||||||
if (trace.enabled) {
|
if (trace.enabled) {
|
||||||
trace.write("NativeScriptActivity.onBackPressed;", trace.categories.NativeLifecycle);
|
trace.write("NativeScriptActivity.onBackPressed;", trace.categories.NativeLifecycle);
|
||||||
}
|
}
|
||||||
@ -856,7 +853,7 @@ class NativeScriptActivity extends android.app.Activity {
|
|||||||
var args = <application.AndroidActivityBackPressedEventData>{
|
var args = <application.AndroidActivityBackPressedEventData>{
|
||||||
eventName: "activityBackPressed",
|
eventName: "activityBackPressed",
|
||||||
object: application.android,
|
object: application.android,
|
||||||
activity: this,
|
activity: activity,
|
||||||
cancel: false,
|
cancel: false,
|
||||||
};
|
};
|
||||||
application.android.notify(args);
|
application.android.notify(args);
|
||||||
@ -866,11 +863,11 @@ class NativeScriptActivity extends android.app.Activity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!frameCommon.goBack()) {
|
if (!frameCommon.goBack()) {
|
||||||
super.onBackPressed();
|
superFunc.call(activity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public onRequestPermissionsResult (requestCode: number, permissions: Array<String>, grantResults: Array<number>): void {
|
public onRequestPermissionsResult(activity: any, requestCode: number, permissions: Array<String>, grantResults: Array<number>, superFunc: Function): void {
|
||||||
if (trace.enabled) {
|
if (trace.enabled) {
|
||||||
trace.write("NativeScriptActivity.onRequestPermissionsResult;", trace.categories.NativeLifecycle);
|
trace.write("NativeScriptActivity.onRequestPermissionsResult;", trace.categories.NativeLifecycle);
|
||||||
}
|
}
|
||||||
@ -878,15 +875,15 @@ class NativeScriptActivity extends android.app.Activity {
|
|||||||
application.android.notify(<application.AndroidActivityRequestPermissionsEventData>{
|
application.android.notify(<application.AndroidActivityRequestPermissionsEventData>{
|
||||||
eventName: "activityRequestPermissions",
|
eventName: "activityRequestPermissions",
|
||||||
object: application.android,
|
object: application.android,
|
||||||
activity: this,
|
activity: activity,
|
||||||
requestCode: requestCode,
|
requestCode: requestCode,
|
||||||
permissions: permissions,
|
permissions: permissions,
|
||||||
grantResults: grantResults
|
grantResults: grantResults
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected onActivityResult(requestCode: number, resultCode: number, data: android.content.Intent): void {
|
public onActivityResult(activity: any, requestCode: number, resultCode: number, data: android.content.Intent, superFunc: Function): void {
|
||||||
super.onActivityResult(requestCode, resultCode, data);
|
superFunc.call(activity, requestCode, resultCode, data);
|
||||||
if (trace.enabled) {
|
if (trace.enabled) {
|
||||||
trace.write(`NativeScriptActivity.onActivityResult(${requestCode}, ${resultCode}, ${data})`, trace.categories.NativeLifecycle);
|
trace.write(`NativeScriptActivity.onActivityResult(${requestCode}, ${resultCode}, ${data})`, trace.categories.NativeLifecycle);
|
||||||
}
|
}
|
||||||
@ -899,10 +896,12 @@ class NativeScriptActivity extends android.app.Activity {
|
|||||||
application.android.notify(<application.AndroidActivityResultEventData>{
|
application.android.notify(<application.AndroidActivityResultEventData>{
|
||||||
eventName: "activityResult",
|
eventName: "activityResult",
|
||||||
object: application.android,
|
object: application.android,
|
||||||
activity: this,
|
activity: activity,
|
||||||
requestCode: requestCode,
|
requestCode: requestCode,
|
||||||
resultCode: resultCode,
|
resultCode: resultCode,
|
||||||
intent: data
|
intent: data
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export var activityCallbacks = new ActivityCallbacksImplementation();
|
13
tns-core-modules/ui/frame/frame.d.ts
vendored
13
tns-core-modules/ui/frame/frame.d.ts
vendored
@ -122,6 +122,8 @@ declare module "ui/frame" {
|
|||||||
on(event: "optionSelected", callback: (args: observable.EventData) => void, thisArg?: any);
|
on(event: "optionSelected", callback: (args: observable.EventData) => void, thisArg?: any);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export var activityCallbacks: AndroidActivityCallbacks;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the topmost frame in the frames stack. An application will typically has one frame instance. Multiple frames handle nested (hierarchical) navigation scenarios.
|
* Gets the topmost frame in the frames stack. An application will typically has one frame instance. Multiple frames handle nested (hierarchical) navigation scenarios.
|
||||||
*/
|
*/
|
||||||
@ -302,6 +304,17 @@ declare module "ui/frame" {
|
|||||||
fragmentForPage(page: pages.Page): any;
|
fragmentForPage(page: pages.Page): any;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface AndroidActivityCallbacks {
|
||||||
|
onCreate(activity: any, savedInstanceState: any, superFunc: Function): void;
|
||||||
|
onSaveInstanceState(activity: any, outState: any, superFunc: Function): void;
|
||||||
|
onStart(activity: any, superFunc: Function): void;
|
||||||
|
onStop(activity: any, superFunc: Function): void;
|
||||||
|
onDestroy(activity: any, superFunc: Function): void;
|
||||||
|
onBackPressed(activity: any, superFunc: Function): void;
|
||||||
|
onRequestPermissionsResult(activity: any, requestCode: number, permissions: Array<String>, grantResults: Array<number>, superFunc: Function): void;
|
||||||
|
onActivityResult(activity: any, requestCode: number, resultCode: number, data: any, superFunc: Function);
|
||||||
|
}
|
||||||
|
|
||||||
/* tslint:disable */
|
/* tslint:disable */
|
||||||
/**
|
/**
|
||||||
* Represents the iOS-specific Frame object, aggregated within the common Frame one.
|
* Represents the iOS-specific Frame object, aggregated within the common Frame one.
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name" : "ui",
|
"name" : "ui",
|
||||||
"nativescript": {}
|
"nativescript": {
|
||||||
|
"recursive-static-bindings": true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -128,6 +128,7 @@
|
|||||||
"tns-core-modules/ui/editable-text-base/editable-text-base.android.ts",
|
"tns-core-modules/ui/editable-text-base/editable-text-base.android.ts",
|
||||||
"tns-core-modules/ui/editable-text-base/editable-text-base.ios.ts",
|
"tns-core-modules/ui/editable-text-base/editable-text-base.ios.ts",
|
||||||
"tns-core-modules/ui/enums/enums.ts",
|
"tns-core-modules/ui/enums/enums.ts",
|
||||||
|
"tns-core-modules/ui/frame/activity.android.ts",
|
||||||
"tns-core-modules/ui/frame/frame-common.ts",
|
"tns-core-modules/ui/frame/frame-common.ts",
|
||||||
"tns-core-modules/ui/frame/frame.android.ts",
|
"tns-core-modules/ui/frame/frame.android.ts",
|
||||||
"tns-core-modules/ui/frame/frame.ios.ts",
|
"tns-core-modules/ui/frame/frame.ios.ts",
|
||||||
|
Reference in New Issue
Block a user