mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
fix(modals): application activityBackPressed event not fired for modals (#6261)
This commit is contained in:
committed by
Dimitar Topuzov
parent
8cd12be59a
commit
13d4f34c1b
@@ -0,0 +1,32 @@
|
||||
import { android as androidApp, AndroidActivityBackPressedEventData } from "tns-core-modules/application";
|
||||
import { fromObject, Observable } from "tns-core-modules/data/observable"
|
||||
|
||||
let context: Observable;
|
||||
function activityBackPressedCallback(args: AndroidActivityBackPressedEventData) {
|
||||
if (context && context.get("shouldCancel")) {
|
||||
context.set("shouldCancel", false);
|
||||
context.set("message", "Back-pressed canceled!");
|
||||
args.cancel = true;
|
||||
}
|
||||
}
|
||||
export function onLoaded(args) {
|
||||
console.log("back-button modal test loaded");
|
||||
context = fromObject({
|
||||
message: "First back-press will be canceled",
|
||||
shouldCancel: true
|
||||
});
|
||||
|
||||
args.object.bindingContext = context;
|
||||
|
||||
if (androidApp) {
|
||||
androidApp.on("activityBackPressed", activityBackPressedCallback);
|
||||
}
|
||||
}
|
||||
|
||||
export function onUnloaded(args) {
|
||||
console.log("back-button modal test unloaded");
|
||||
|
||||
if (androidApp) {
|
||||
androidApp.off("activityBackPressed", activityBackPressedCallback);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<StackLayout loaded="onLoaded" unloaded="onUnloaded">
|
||||
<Label text="{{ message }}" />
|
||||
<Label text="{{ 'will cancel next back press: ' + shouldCancel }}" />
|
||||
</StackLayout>
|
||||
@@ -57,6 +57,15 @@ export function onModalLayout(args: EventData) {
|
||||
false);
|
||||
}
|
||||
|
||||
export function onAndroidBackEvents(args: EventData) {
|
||||
const view = args.object as View;
|
||||
view.showModal(
|
||||
"android-back-button/android-back-button-page",
|
||||
null,
|
||||
() => console.log("android-back-button modal page layout closed"),
|
||||
true, true, true);
|
||||
}
|
||||
|
||||
export function onModalTabView(args: EventData) {
|
||||
const fullscreen = false;
|
||||
const animated = false;
|
||||
|
||||
@@ -19,5 +19,6 @@
|
||||
<Button text="Reset Frame Root View" tap="onFrameRootViewReset" />
|
||||
<Button text="Reset Tab Root View" tap="onTabRootViewReset" />
|
||||
<Button text="Reset Layout Root View" tap="onLayoutRootViewReset" />
|
||||
<Button text="Android Back Btn Events" tap="onAndroidBackEvents" />
|
||||
</StackLayout>
|
||||
</Page>
|
||||
|
||||
Reference in New Issue
Block a user