From 7a197a671162222c1db4ff33abf3371b027d3be7 Mon Sep 17 00:00:00 2001 From: Rossen Hristov Date: Mon, 6 Jul 2015 11:54:45 +0300 Subject: [PATCH] Resolved Issue #391: Added cancellable Android activityBackPressed event + fixed the names of the other activity events because they were wrong. --- application/application.android.ts | 18 +++++++++--------- application/application.d.ts | 20 ++++++++++++++++++++ apps/tests/app/app.ts | 4 ++++ ui/frame/frame.android.ts | 13 +++++++++++++ 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/application/application.android.ts b/application/application.android.ts index 8389aa12c..d766d0bbc 100644 --- a/application/application.android.ts +++ b/application/application.android.ts @@ -11,14 +11,15 @@ var callbacks = android.app.Application.ActivityLifecycleCallbacks; export var mainModule: string; -export var androidActivityCreatedEvent = "androidActivityCreated"; -export var androidActivityDestroyedEvent = "androidActivityDestroyed"; -export var androidActivityStartedEvent = "androidActivityStarted"; -export var androidActivityPausedEvent = "androidActivityPaused"; -export var androidActivityResumedEvent = "androidActivityResumed"; -export var androidActivityStoppedEvent = "androidActivityStopped"; -export var androidSaveActivityStateEvent = "androidSaveActivityState"; -export var androidActivityResultEvent = "androidActivityResult"; +export var androidActivityCreatedEvent = "activityCreated"; +export var androidActivityDestroyedEvent = "activityDestroyed"; +export var androidActivityStartedEvent = "activityStarted"; +export var androidActivityPausedEvent = "activityPaused"; +export var androidActivityResumedEvent = "activityResumed"; +export var androidActivityStoppedEvent = "activityStopped"; +export var androidSaveActivityStateEvent = "saveActivityState"; +export var androidActivityResultEvent = "activityResult"; +export var androidActivityBackPressedEvent = "activityBackPressed"; // We are using the exports object for the common events since we merge the appModule with this module's exports, which is what users will receive when require("application") is called; // TODO: This is kind of hacky and is "pure JS in TypeScript" @@ -163,7 +164,6 @@ class AndroidApplication implements dts.AndroidApplication { public onActivityResumed: (activity: android.app.Activity) => void; public onActivityStopped: (activity: android.app.Activity) => void; public onSaveActivityState: (activity: android.app.Activity, bundle: android.os.Bundle) => void; - public onActivityResult: (requestCode: number, resultCode: number, data: android.content.Intent) => void; private _eventsToken: any; diff --git a/application/application.d.ts b/application/application.d.ts index 45693f40a..09d57ccd5 100644 --- a/application/application.d.ts +++ b/application/application.d.ts @@ -250,6 +250,16 @@ declare module "application" { intent: android.content.Intent; } + /** + * Data for the Android activity back pressed event. + */ + export interface AndroidActivityBackPressedEventData extends AndroidActivityEventData { + /** + * In the event handler, set this value to true if you want to cancel the back navigation and do something else instead. + */ + cancel: boolean; + } + /** * This event is raised on android application ActivityCreated. */ @@ -290,6 +300,11 @@ declare module "application" { */ export function on(event: "activityResult", callback: (args: AndroidActivityResultEventData) => void, thisArg?: any); + /** + * This event is raised on the back button is pressed in an android application. + */ + export function on(event: "activityBackPressed", callback: (args: AndroidActivityBackPressedEventData) => void, thisArg?: any); + /** * String value used when hooking to ActivityCreated event. */ @@ -330,6 +345,11 @@ declare module "application" { */ export var androidActivityResultEvent: string; + /** + * String value used when hooking to ActivityBackPressed event. + */ + export var androidActivityBackPressedEvent: string; + /** * The abstraction of an Android-specific application object. */ diff --git a/apps/tests/app/app.ts b/apps/tests/app/app.ts index 4f21ef707..2357b3271 100644 --- a/apps/tests/app/app.ts +++ b/apps/tests/app/app.ts @@ -98,6 +98,10 @@ if (platform.device.os === platform.platformNames.android) { application.on(application.androidSaveActivityStateEvent, function (args: application.AndroidActivityBundleEventData) { console.log("Event: " + args.eventName + ", Activity: " + args.activity + ", Bundle: " + args.bundle); }); + + application.on(application.androidActivityBackPressedEvent, function (args: application.AndroidActivityBackPressedEventData) { + console.log("Event: " + args.eventName + ", Activity: " + args.activity); + }); } application.start(); diff --git a/ui/frame/frame.android.ts b/ui/frame/frame.android.ts index 441790041..2abaed24e 100644 --- a/ui/frame/frame.android.ts +++ b/ui/frame/frame.android.ts @@ -494,6 +494,19 @@ var NativeActivity = { onBackPressed: function () { trace.write("NativeScriptActivity.onBackPressed;", trace.categories.NativeLifecycle); + + var args = { + eventName: application.androidActivityBackPressedEvent, + object: application.android, + activity: this, + cancel: false, + }; + application.notify(args); + + if (args.cancel) { + return; + } + if (!frameCommon.goBack()) { this.super.onBackPressed(); }