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;
|
||||
var framesCache: Array<WeakRef<AndroidFrame>> = new Array<WeakRef<AndroidFrame>>();
|
||||
let framesCounter = 0;
|
||||
let framesCache: Array<WeakRef<AndroidFrame>> = new Array<WeakRef<AndroidFrame>>();
|
||||
|
||||
class AndroidFrame extends Observable implements definition.AndroidFrame {
|
||||
public rootViewGroup: android.view.ViewGroup;
|
||||
@ -600,7 +600,7 @@ function getFrameById(frameId: number): Frame {
|
||||
return null;
|
||||
}
|
||||
|
||||
var animationFixed;
|
||||
let animationFixed;
|
||||
function ensureAnimationFixed() {
|
||||
if (!animationFixed) {
|
||||
// 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 NativeScriptActivity extends android.app.Activity {
|
||||
private rootView: View;
|
||||
class ActivityCallbacksImplementation implements definition.AndroidActivityCallbacks {
|
||||
private _rootView: View;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
return global.__native(this);
|
||||
}
|
||||
|
||||
protected onCreate(savedInstanceState: android.os.Bundle): void {
|
||||
public onCreate(activity: android.app.Activity, savedInstanceState: android.os.Bundle, superFunc: Function): void {
|
||||
if (trace.enabled) {
|
||||
trace.write(`NativeScriptActivity.onCreate(${savedInstanceState})`, trace.categories.NativeLifecycle);
|
||||
trace.write(`Activity.onCreate(${savedInstanceState})`, trace.categories.NativeLifecycle);
|
||||
}
|
||||
|
||||
let app = application.android;
|
||||
let intent = this.getIntent();
|
||||
let intent = activity.getIntent();
|
||||
if (application.onLaunch) {
|
||||
application.onLaunch(intent);
|
||||
}
|
||||
|
||||
let args: application.LaunchEventData = { eventName: application.launchEvent, object: app, android: intent };
|
||||
application.notify(args);
|
||||
let launchArgs: application.LaunchEventData = { eventName: application.launchEvent, object: app, android: intent };
|
||||
application.notify(launchArgs);
|
||||
|
||||
let frameId = -1;
|
||||
let rootView = args.root;
|
||||
let rootView = launchArgs.root;
|
||||
let extras = intent.getExtras();
|
||||
|
||||
// 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.
|
||||
// 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.
|
||||
var isRestart = !!savedInstanceState && activityInitialized;
|
||||
super.onCreate(isRestart ? savedInstanceState : null);
|
||||
let isRestart = !!savedInstanceState && activityInitialized;
|
||||
superFunc.call(activity, isRestart ? savedInstanceState : null);
|
||||
|
||||
this.rootView = rootView;
|
||||
this._rootView = rootView;
|
||||
|
||||
// Initialize native visual tree;
|
||||
rootView._onAttached(this);
|
||||
this.setContentView(rootView._nativeView, new org.nativescript.widgets.CommonLayoutParams());
|
||||
rootView._onAttached(activity);
|
||||
activity.setContentView(rootView._nativeView, new org.nativescript.widgets.CommonLayoutParams());
|
||||
// frameId is negative w
|
||||
if (frame) {
|
||||
frame.navigate(navParam);
|
||||
@ -806,49 +800,52 @@ class NativeScriptActivity extends android.app.Activity {
|
||||
activityInitialized = true;
|
||||
}
|
||||
|
||||
protected onSaveInstanceState(outState: android.os.Bundle): void {
|
||||
super.onSaveInstanceState(outState);
|
||||
let view = this.rootView;
|
||||
public onSaveInstanceState(activity: android.app.Activity, outState: android.os.Bundle, superFunc: Function): void {
|
||||
superFunc.call(activity, outState);
|
||||
let view = this._rootView;
|
||||
if (view instanceof Frame) {
|
||||
outState.putInt(INTENT_EXTRA, view.android.frameId);
|
||||
}
|
||||
}
|
||||
|
||||
protected onStart(): void {
|
||||
super.onStart();
|
||||
public onStart(activity: any, superFunc: Function): void {
|
||||
superFunc.call(activity);
|
||||
|
||||
if (trace.enabled) {
|
||||
trace.write("NativeScriptActivity.onStart();", trace.categories.NativeLifecycle);
|
||||
}
|
||||
let rootView = this.rootView
|
||||
let rootView = this._rootView;
|
||||
if (rootView && !rootView.isLoaded) {
|
||||
rootView.onLoaded();
|
||||
}
|
||||
}
|
||||
|
||||
protected onStop(): void {
|
||||
super.onStop();
|
||||
public onStop(activity: any, superFunc: Function): void {
|
||||
superFunc.call(activity);
|
||||
|
||||
if (trace.enabled) {
|
||||
trace.write("NativeScriptActivity.onStop();", trace.categories.NativeLifecycle);
|
||||
}
|
||||
let rootView = this.rootView
|
||||
let rootView = this._rootView;
|
||||
if (rootView && rootView.isLoaded) {
|
||||
rootView.onUnloaded();
|
||||
}
|
||||
}
|
||||
|
||||
protected onDestroy(): void {
|
||||
let rootView = this.rootView
|
||||
public onDestroy(activity: any, superFunc: Function): void {
|
||||
let rootView = this._rootView
|
||||
if (rootView && rootView._context) {
|
||||
rootView._onDetached(true);
|
||||
}
|
||||
|
||||
super.onDestroy();
|
||||
superFunc.call(activity);
|
||||
|
||||
if (trace.enabled) {
|
||||
trace.write("NativeScriptActivity.onDestroy();", trace.categories.NativeLifecycle);
|
||||
}
|
||||
}
|
||||
|
||||
public onBackPressed(): void {
|
||||
public onBackPressed(activity: any, superFunc: Function): void {
|
||||
if (trace.enabled) {
|
||||
trace.write("NativeScriptActivity.onBackPressed;", trace.categories.NativeLifecycle);
|
||||
}
|
||||
@ -856,7 +853,7 @@ class NativeScriptActivity extends android.app.Activity {
|
||||
var args = <application.AndroidActivityBackPressedEventData>{
|
||||
eventName: "activityBackPressed",
|
||||
object: application.android,
|
||||
activity: this,
|
||||
activity: activity,
|
||||
cancel: false,
|
||||
};
|
||||
application.android.notify(args);
|
||||
@ -866,11 +863,11 @@ class NativeScriptActivity extends android.app.Activity {
|
||||
}
|
||||
|
||||
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) {
|
||||
trace.write("NativeScriptActivity.onRequestPermissionsResult;", trace.categories.NativeLifecycle);
|
||||
}
|
||||
@ -878,15 +875,15 @@ class NativeScriptActivity extends android.app.Activity {
|
||||
application.android.notify(<application.AndroidActivityRequestPermissionsEventData>{
|
||||
eventName: "activityRequestPermissions",
|
||||
object: application.android,
|
||||
activity: this,
|
||||
activity: activity,
|
||||
requestCode: requestCode,
|
||||
permissions: permissions,
|
||||
grantResults: grantResults
|
||||
});
|
||||
}
|
||||
|
||||
protected onActivityResult(requestCode: number, resultCode: number, data: android.content.Intent): void {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
public onActivityResult(activity: any, requestCode: number, resultCode: number, data: android.content.Intent, superFunc: Function): void {
|
||||
superFunc.call(activity, requestCode, resultCode, data);
|
||||
if (trace.enabled) {
|
||||
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>{
|
||||
eventName: "activityResult",
|
||||
object: application.android,
|
||||
activity: this,
|
||||
activity: activity,
|
||||
requestCode: requestCode,
|
||||
resultCode: resultCode,
|
||||
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);
|
||||
}
|
||||
|
||||
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.
|
||||
*/
|
||||
@ -302,6 +304,17 @@ declare module "ui/frame" {
|
||||
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 */
|
||||
/**
|
||||
* Represents the iOS-specific Frame object, aggregated within the common Frame one.
|
||||
|
@ -1,4 +1,6 @@
|
||||
{
|
||||
"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.ios.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.android.ts",
|
||||
"tns-core-modules/ui/frame/frame.ios.ts",
|
||||
|
Reference in New Issue
Block a user