FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(modals): application activityBackPressed event not fired for moda… · NativeScript/NativeScript@8575c60 · GitHub

Commit 8575c60

Browse files
Alexander Vakrilov
committed
fix(modals): application activityBackPressed event not fired for modals (#6261)
1 parent 8a5f730 commit 8575c60

5 files changed

Lines changed: 54 additions & 1 deletion

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { android as androidApp, AndroidActivityBackPressedEventData } from "tns-core-modules/application";
2+
import { fromObject, Observable } from "tns-core-modules/data/observable"
3+
4+
let context: Observable;
5+
function activityBackPressedCallback(args: AndroidActivityBackPressedEventData) {
6+
if (context && context.get("shouldCancel")) {
7+
context.set("shouldCancel", false);
8+
context.set("message", "Back-pressed canceled!");
9+
args.cancel = true;
10+
}
11+
}
12+
export function onLoaded(args) {
13+
console.log("back-button modal test loaded");
14+
context = fromObject({
15+
message: "First back-press will be canceled",
16+
shouldCancel: true
17+
});
18+
19+
args.object.bindingContext = context;
20+
21+
if (androidApp) {
22+
androidApp.on("activityBackPressed", activityBackPressedCallback);
23+
}
24+
}
25+
26+
export function onUnloaded(args) {
27+
console.log("back-button modal test unloaded");
28+
29+
if (androidApp) {
30+
androidApp.off("activityBackPressed", activityBackPressedCallback);
31+
}
32+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<StackLayout loaded="onLoaded" unloaded="onUnloaded">
2+
<Label text="{{ message }}" />
3+
<Label text="{{ 'will cancel next back press: ' + shouldCancel }}" />
4+
</StackLayout>

‎e2e/modal-navigation/app/home/home-page.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ export function onModalLayout(args: EventData) {
5757
false);
5858
}
5959

60+
export function onAndroidBackEvents(args: EventData) {
61+
const view = args.object as View;
62+
view.showModal(
63+
"android-back-button/android-back-button-page",
64+
null,
65+
() => console.log("android-back-button modal page layout closed"),
66+
true, true, true);
67+
}
68+
6069
export function onModalTabView(args: EventData) {
6170
const fullscreen = false;
6271
const animated = false;

‎e2e/modal-navigation/app/home/home-page.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
<Button text="Reset Frame Root View" tap="onFrameRootViewReset" />
2020
<Button text="Reset Tab Root View" tap="onTabRootViewReset" />
2121
<Button text="Reset Layout Root View" tap="onLayoutRootViewReset" />
22+
<Button text="Android Back Btn Events" tap="onAndroidBackEvents" />
2223
</StackLayout>
2324
</Page>

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Definitions.
22
import { Point, CustomLayoutView as CustomLayoutViewDefinition, dip } from ".";
33
import { GestureTypes, GestureEventData } from "../../gestures";
4-
import { AndroidActivityBackPressedEventData } from "../../../application";
54
// Types.
65
import {
76
ViewCommon, layout, isEnabledProperty, originXProperty, originYProperty, automationTextProperty, isUserInteractionEnabledProperty,
@@ -22,6 +21,7 @@ import {
2221
import { Background, ad as androidBackground } from "../../styling/background";
2322
import { profile } from "../../../profiling";
2423
import { topmost } from "../../frame/frame-stack";
24+
import { AndroidActivityBackPressedEventData, android as androidApp } from "../../../application";
2525

2626
export * from "./view-common";
2727

@@ -107,6 +107,13 @@ function initializeDialogFragment() {
107107
activity: view._context,
108108
cancel: false,
109109
};
110+
111+
// Fist fire application.android global event
112+
androidApp.notify(args);
113+
if (args.cancel) {
114+
return;
115+
}
116+
110117
view.notify(args);
111118

112119
if (!args.cancel && !view.onBackPressed()) {

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL