Resolved Issue #391: Added cancellable Android activityBackPressed event + fixed the names of the other activity events because they were wrong.

This commit is contained in:
Rossen Hristov
2015-07-06 11:54:45 +03:00
parent 95b616edb3
commit 7a197a6711
4 changed files with 46 additions and 9 deletions

View File

@@ -11,14 +11,15 @@ var callbacks = android.app.Application.ActivityLifecycleCallbacks;
export var mainModule: string; export var mainModule: string;
export var androidActivityCreatedEvent = "androidActivityCreated"; export var androidActivityCreatedEvent = "activityCreated";
export var androidActivityDestroyedEvent = "androidActivityDestroyed"; export var androidActivityDestroyedEvent = "activityDestroyed";
export var androidActivityStartedEvent = "androidActivityStarted"; export var androidActivityStartedEvent = "activityStarted";
export var androidActivityPausedEvent = "androidActivityPaused"; export var androidActivityPausedEvent = "activityPaused";
export var androidActivityResumedEvent = "androidActivityResumed"; export var androidActivityResumedEvent = "activityResumed";
export var androidActivityStoppedEvent = "androidActivityStopped"; export var androidActivityStoppedEvent = "activityStopped";
export var androidSaveActivityStateEvent = "androidSaveActivityState"; export var androidSaveActivityStateEvent = "saveActivityState";
export var androidActivityResultEvent = "androidActivityResult"; 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; // 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" // 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 onActivityResumed: (activity: android.app.Activity) => void;
public onActivityStopped: (activity: android.app.Activity) => void; public onActivityStopped: (activity: android.app.Activity) => void;
public onSaveActivityState: (activity: android.app.Activity, bundle: android.os.Bundle) => void; public onSaveActivityState: (activity: android.app.Activity, bundle: android.os.Bundle) => void;
public onActivityResult: (requestCode: number, resultCode: number, data: android.content.Intent) => void; public onActivityResult: (requestCode: number, resultCode: number, data: android.content.Intent) => void;
private _eventsToken: any; private _eventsToken: any;

View File

@@ -250,6 +250,16 @@ declare module "application" {
intent: android.content.Intent; 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. * 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); 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. * String value used when hooking to ActivityCreated event.
*/ */
@@ -330,6 +345,11 @@ declare module "application" {
*/ */
export var androidActivityResultEvent: string; export var androidActivityResultEvent: string;
/**
* String value used when hooking to ActivityBackPressed event.
*/
export var androidActivityBackPressedEvent: string;
/** /**
* The abstraction of an Android-specific application object. * The abstraction of an Android-specific application object.
*/ */

View File

@@ -98,6 +98,10 @@ if (platform.device.os === platform.platformNames.android) {
application.on(application.androidSaveActivityStateEvent, function (args: application.AndroidActivityBundleEventData) { application.on(application.androidSaveActivityStateEvent, function (args: application.AndroidActivityBundleEventData) {
console.log("Event: " + args.eventName + ", Activity: " + args.activity + ", Bundle: " + args.bundle); 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(); application.start();

View File

@@ -494,6 +494,19 @@ var NativeActivity = {
onBackPressed: function () { onBackPressed: function () {
trace.write("NativeScriptActivity.onBackPressed;", trace.categories.NativeLifecycle); trace.write("NativeScriptActivity.onBackPressed;", trace.categories.NativeLifecycle);
var args = <application.AndroidActivityBackPressedEventData>{
eventName: application.androidActivityBackPressedEvent,
object: application.android,
activity: this,
cancel: false,
};
application.notify(args);
if (args.cancel) {
return;
}
if (!frameCommon.goBack()) { if (!frameCommon.goBack()) {
this.super.onBackPressed(); this.super.onBackPressed();
} }