Skip to content

Commit 3efc06e

Browse files
EddyVerbruggenMartoYankov
authored andcommitted
feat(android): implement a 'activityNewIntent' event
1 parent 2d63c9f commit 3efc06e

6 files changed

Lines changed: 77 additions & 18 deletions

File tree

‎tests/app/app/app.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ if (application.android) {
135135
console.log("Event: " + args.eventName + ", Activity: " + args.activity);
136136
// Set args.cancel = true to cancel back navigation and do something custom.
137137
});
138+
139+
application.android.on(application.AndroidApplication.activityNewIntentEvent, function (args: application.AndroidActivityNewIntentEventData) {
140+
console.log("Event: " + args.eventName + ", Activity: " + args.activity + ", Intent: " + args.intent);
141+
});
138142
}
139143

140144
var time;

‎tns-core-modules/application/application.android.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
AndroidActivityBundleEventData, AndroidActivityEventData, ApplicationEventData, OrientationChangedEventData,
3-
AndroidApplication as AndroidApplicationDefinition,
3+
AndroidApplication as AndroidApplicationDefinition, AndroidActivityNewIntentEventData,
44
AndroidActivityResultEventData, AndroidActivityBackPressedEventData, AndroidActivityRequestPermissionsEventData
55
} from ".";
66

@@ -25,6 +25,7 @@ const ActivityStopped = "activityStopped";
2525
const SaveActivityState = "saveActivityState";
2626
const ActivityResult = "activityResult";
2727
const ActivityBackPressed = "activityBackPressed";
28+
const ActivityNewIntent = "activityNewIntent";
2829
const ActivityRequestPermissions = "activityRequestPermissions";
2930

3031
export class AndroidApplication extends Observable implements AndroidApplicationDefinition {
@@ -37,6 +38,7 @@ export class AndroidApplication extends Observable implements AndroidApplication
3738
public static saveActivityStateEvent = SaveActivityState;
3839
public static activityResultEvent = ActivityResult;
3940
public static activityBackPressedEvent = ActivityBackPressed;
41+
public static activityNewIntentEvent = ActivityNewIntent;
4042
public static activityRequestPermissionsEvent = ActivityRequestPermissions;
4143

4244
public paused: boolean;
@@ -119,6 +121,7 @@ export interface AndroidApplication {
119121
on(event: "saveActivityState", callback: (args: AndroidActivityBundleEventData) => void, thisArg?: any);
120122
on(event: "activityResult", callback: (args: AndroidActivityResultEventData) => void, thisArg?: any);
121123
on(event: "activityBackPressed", callback: (args: AndroidActivityBackPressedEventData) => void, thisArg?: any);
124+
on(event: "activityNewIntent", callback: (args: AndroidActivityNewIntentEventData) => void, thisArg?: any);
122125
on(event: "activityRequestPermissions", callback: (args: AndroidActivityRequestPermissionsEventData) => void, thisArg?: any);
123126
}
124127

‎tns-core-modules/application/application.d.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export function off(eventNames: string, callback?: any, thisArg?: any);
187187

188188
/**
189189
* @deprecated use application.run() instead.
190-
*
190+
*
191191
* Call this method to start the application. Important: All code after this method call will not be executed!
192192
*/
193193
export function start(entry?: NavigationEntry | string);
@@ -371,6 +371,16 @@ export interface AndroidActivityResultEventData extends AndroidActivityEventData
371371
intent: any /* android.content.Intent */;
372372
}
373373

374+
/**
375+
* Data for the Android activity newIntent event.
376+
*/
377+
export interface AndroidActivityNewIntentEventData extends AndroidActivityEventData {
378+
/**
379+
* The intent.
380+
*/
381+
intent: any /* android.content.Intent */;
382+
}
383+
374384
/**
375385
* Data for the Android activity back pressed event.
376386
*/
@@ -480,6 +490,11 @@ export class AndroidApplication extends Observable {
480490
*/
481491
on(event: "activityBackPressed", callback: (args: AndroidActivityBackPressedEventData) => void, thisArg?: any);
482492

493+
/**
494+
* This event is raised when the Android app was launched by an Intent with data.
495+
*/
496+
on(event: "activityNewIntent", callback: (args: AndroidActivityNewIntentEventData) => void, thisArg?: any);
497+
483498
/**
484499
* This event is raised when the Android activity requests permissions.
485500
*/
@@ -530,6 +545,11 @@ export class AndroidApplication extends Observable {
530545
*/
531546
public static activityBackPressedEvent: string;
532547

548+
/**
549+
* String value used when hooking to activityNewIntent event.
550+
*/
551+
public static activityNewIntentEvent: string;
552+
533553
/**
534554
* String value used when hooking to requestPermissions event.
535555
*/

‎tns-core-modules/ui/frame/activity.android.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ class NativeScriptActivity extends android.support.v7.app.AppCompatActivity {
2626
setActivityCallbacks(this);
2727
}
2828

29-
this._callbacks.onCreate(this, savedInstanceState, super.onCreate);
29+
this._callbacks.onCreate(this, savedInstanceState, this.getIntent(), super.onCreate);
3030
}
3131

3232
public onNewIntent(intent: android.content.Intent): void {
33-
super.onNewIntent(intent);
34-
super.setIntent(intent);
33+
this._callbacks.onNewIntent(this, intent, super.setIntent, super.onNewIntent);
3534
}
3635

3736
public onSaveInstanceState(outState: android.os.Bundle): void {

‎tns-core-modules/ui/frame/frame.android.ts‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -944,11 +944,17 @@ class ActivityCallbacksImplementation implements AndroidActivityCallbacks {
944944
}
945945

946946
@profile
947-
public onCreate(activity: android.support.v7.app.AppCompatActivity, savedInstanceState: android.os.Bundle, superFunc: Function): void {
947+
public onCreate(activity: android.support.v7.app.AppCompatActivity, savedInstanceState: android.os.Bundle, intentOrSuperFunc: android.content.Intent | Function, superFunc?: Function): void {
948948
if (traceEnabled()) {
949949
traceWrite(`Activity.onCreate(${savedInstanceState})`, traceCategories.NativeLifecycle);
950950
}
951951

952+
const intent: android.content.Intent = superFunc ? <android.content.Intent>intentOrSuperFunc : undefined;
953+
954+
if (!superFunc) {
955+
superFunc = <Function>intentOrSuperFunc;
956+
}
957+
952958
// If there is savedInstanceState this call will recreate all fragments that were previously in the navigation.
953959
// We take care of associating them with a Page from our backstack in the onAttachFragment callback.
954960
// If there is savedInstanceState and moduleLoaded is false we are restarted but process was killed.
@@ -966,6 +972,15 @@ class ActivityCallbacksImplementation implements AndroidActivityCallbacks {
966972
}
967973
}
968974

975+
if (intent && intent.getAction()) {
976+
application.android.notify(<application.AndroidActivityNewIntentEventData>{
977+
eventName: application.AndroidApplication.activityNewIntentEvent,
978+
object: application.android,
979+
activity,
980+
intent
981+
});
982+
}
983+
969984
this.setActivityContent(activity, savedInstanceState, true);
970985
moduleLoaded = true;
971986
}
@@ -982,6 +997,19 @@ class ActivityCallbacksImplementation implements AndroidActivityCallbacks {
982997
outState.putInt(ROOT_VIEW_ID_EXTRA, rootView._domId);
983998
}
984999

1000+
@profile
1001+
public onNewIntent(activity: android.support.v7.app.AppCompatActivity, intent: android.content.Intent, superSetIntentFunc: Function, superFunc: Function): void {
1002+
superFunc.call(activity, intent);
1003+
superSetIntentFunc.call(activity, intent);
1004+
1005+
application.android.notify(<application.AndroidActivityNewIntentEventData>{
1006+
eventName: application.AndroidApplication.activityNewIntentEvent,
1007+
object: application.android,
1008+
activity,
1009+
intent
1010+
});
1011+
}
1012+
9851013
@profile
9861014
public onStart(activity: any, superFunc: Function): void {
9871015
superFunc.call(activity);
@@ -1188,25 +1216,25 @@ class ActivityCallbacksImplementation implements AndroidActivityCallbacks {
11881216
if (shouldCreateRootFrame) {
11891217
const extras = intent.getExtras();
11901218
let frameId = -1;
1191-
1219+
11921220
// We have extras when we call - new Frame().navigate();
11931221
// savedInstanceState is used when activity is recreated.
11941222
// NOTE: On API 23+ we get extras on first run.
11951223
// Check changed - first try to get frameId from Extras if not from saveInstanceState.
11961224
if (extras) {
11971225
frameId = extras.getInt(INTENT_EXTRA, -1);
11981226
}
1199-
1227+
12001228
if (savedInstanceState && frameId < 0) {
12011229
frameId = savedInstanceState.getInt(INTENT_EXTRA, -1);
12021230
}
1203-
1231+
12041232
if (!rootView) {
12051233
// If we have frameId from extras - we are starting a new activity from navigation (e.g. new Frame().navigate()))
12061234
// Then we check if we have frameId from savedInstanceState - this happens when Activity is destroyed but app was not (e.g. suspend)
12071235
rootView = getFrameByNumberId(frameId) || new Frame();
12081236
}
1209-
1237+
12101238
if (rootView instanceof Frame) {
12111239
rootView.navigate(mainEntry);
12121240
} else {

‎tns-core-modules/ui/frame/frame.d.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export * from "../page";
1616
export class Frame extends View {
1717
/**
1818
* @deprecated
19-
*
19+
*
2020
* String value used when hooking to androidOptionSelected event (prefix `android` states that this event is available only in Android).
2121
*/
2222
public static androidOptionSelectedEvent: string;
@@ -33,7 +33,7 @@ export class Frame extends View {
3333
canGoBack(): boolean;
3434

3535
/**
36-
* Navigates to a Page instance as described by the module name.
36+
* Navigates to a Page instance as described by the module name.
3737
* This method will require the module and will check for a Page property in the exports of the module.
3838
* @param pageModuleName The name of the module to require starting from the application root.
3939
* For example if you want to navigate to page called "myPage.js" in a folder called "subFolder" and your root folder is "app" you can call navigate method like this:
@@ -175,7 +175,7 @@ export class Frame extends View {
175175

176176
/**
177177
* A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
178-
* @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").
178+
* @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").
179179
* @param callback - Callback function which will be executed when event is raised.
180180
* @param thisArg - An optional parameter which will be used as `this` context for callback execution.
181181
*/
@@ -210,7 +210,7 @@ export function goBack();
210210

211211
/**
212212
* @deprecated use getFrameById() if you want to retrieve a frame different than the topmost one
213-
*
213+
*
214214
* Gets the frames stack.
215215
*/
216216
export function stack(): Array<Frame>;
@@ -264,7 +264,7 @@ export interface NavigationEntry extends ViewEntry {
264264
transitionAndroid?: NavigationTransition;
265265

266266
/**
267-
* True to record the navigation in the backstack, false otherwise.
267+
* True to record the navigation in the backstack, false otherwise.
268268
* If the parameter is set to false then the Page will be displayed but once navigated from it will not be able to be navigated back to.
269269
*/
270270
backstackVisible?: boolean;
@@ -350,7 +350,7 @@ export interface BackstackEntry {
350350
}
351351

352352
/**
353-
* Represents the data passed to the androidOptionSelected event.
353+
* Represents the data passed to the androidOptionSelected event.
354354
* This event is raised by the Android OS when an option in the Activity's action bar has been selected.
355355
*/
356356
export interface AndroidOptionEventData extends EventData {
@@ -398,7 +398,7 @@ export interface AndroidFrame extends Observable {
398398

399399
/**
400400
* @deprecated this property is not used internally
401-
*
401+
*
402402
* Gets or sets whether the page UI will be cached when navigating away from the page.
403403
*/
404404
cachePagesOnNavigate: boolean;
@@ -413,8 +413,12 @@ export interface AndroidFrame extends Observable {
413413
export interface AndroidActivityCallbacks {
414414
getRootView(): View;
415415
resetActivityContent(activity: any): void;
416-
416+
417+
/**
418+
* @deprecated use onCreate(activity, savedInstanceState, intent, superFunc) instead.
419+
*/
417420
onCreate(activity: any, savedInstanceState: any, superFunc: Function): void;
421+
onCreate(activity: any, savedInstanceState: any, intent: any, superFunc: Function): void;
418422
onSaveInstanceState(activity: any, outState: any, superFunc: Function): void;
419423
onStart(activity: any, superFunc: Function): void;
420424
onStop(activity: any, superFunc: Function): void;
@@ -423,6 +427,7 @@ export interface AndroidActivityCallbacks {
423427
onBackPressed(activity: any, superFunc: Function): void;
424428
onRequestPermissionsResult(activity: any, requestCode: number, permissions: Array<String>, grantResults: Array<number>, superFunc: Function): void;
425429
onActivityResult(activity: any, requestCode: number, resultCode: number, data: any, superFunc: Function);
430+
onNewIntent(activity: any, intent: any, superSetIntentFunc: Function, superFunc: Function): void;
426431
}
427432

428433
export interface AndroidFragmentCallbacks {

0 commit comments

Comments
 (0)