Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/references.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/// <reference path="../types-ios/src/lib/ios/objc-x86_64/objc!Darwin.d.ts" />
/// <reference path="../types-ios/src/lib/ios/objc-x86_64/objc!DarwinFoundation.d.ts" />
/// <reference path="../types-ios/src/lib/ios/objc-x86_64/objc!Symbols.d.ts" />
/// <reference path="../types-android/src/lib/android-30.d.ts" />
/// <reference path="../types-android/src/lib/android-33.d.ts" />
/// <reference path="./platforms/ios/typings/objc!MaterialComponents.d.ts" />
/// <reference path="./platforms/ios/typings/objc!NativeScriptUtils.d.ts" />
/// <reference path="./global-types.d.ts" />
61 changes: 60 additions & 1 deletion packages/core/ui/core/view/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,51 @@ interface DialogOptions {
dismissCallback: () => void;
}

let OnBackPressedCallback;

if (SDK_VERSION >= 33) {
OnBackPressedCallback = (androidx.activity.OnBackPressedCallback as any).extend({
handleOnBackPressed() {
console.log('OnBackPressedCallback handleOnBackPressed called');
const dialog = this['_dialog']?.get();

if (!dialog) {
// disable the callback and call super to avoid infinite loop

this.setEnabled(false);

return;
}

const view = dialog.fragment.owner;

const args: AndroidActivityBackPressedEventData = {
eventName: 'activityBackPressed',
object: view,
activity: view._context,
cancel: false,
};

// Fist fire application.android global event
getNativeScriptGlobals().events.notify(args);

if (args.cancel) {
return;
}

view.notify(args);

if (!args.cancel) {
this.setEnabled(false);

dialog.getOnBackPressedDispatcher().onBackPressed();

this.setEnabled(true);
}
},
});
}

interface TouchListener {
new (owner: View): android.view.View.OnTouchListener;
}
Expand Down Expand Up @@ -121,14 +166,24 @@ function initializeDialogFragment() {
}

@NativeClass
class DialogImpl extends android.app.Dialog {
class DialogImpl extends androidx.appcompat.app.AppCompatDialog {
constructor(
public fragment: DialogFragmentImpl,
context: android.content.Context,
themeResId: number,
) {
super(context, themeResId);

if (SDK_VERSION >= 33 && OnBackPressedCallback) {
const callback = new OnBackPressedCallback(true);

callback['_dialog'] = new WeakRef(this);

// @ts-ignore

this.getOnBackPressedDispatcher().addCallback(this, callback);
}

return global.__native(this);
}

Expand All @@ -138,6 +193,10 @@ function initializeDialogFragment() {
}

public onBackPressed(): void {
if (SDK_VERSION >= 33) {
super.onBackPressed();
return;
}
const view = this.fragment.owner;
const args = <AndroidActivityBackPressedEventData>{
eventName: 'activityBackPressed',
Expand Down
82 changes: 82 additions & 0 deletions packages/core/ui/frame/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { AndroidActivityBackPressedEventData, AndroidActivityNewIntentEventData,
import { Application } from '../../application/application';
import { isEmbedded, setEmbeddedView } from '../embedding';
import { CALLBACKS, FRAMEID, framesCache, setFragmentCallbacks } from './frame-helper-for-android';
import { Device } from '../../platform';

export * from './frame-common';
export { setFragmentClass } from './fragment';
Expand Down Expand Up @@ -762,6 +763,78 @@ function startActivity(activity: androidx.appcompat.app.AppCompatActivity, frame
activity.startActivity(intent);
}

let OnBackPressedCallback;

if (parseInt(Device.sdkVersion) >= 33) {
OnBackPressedCallback = (<any>androidx.activity.OnBackPressedCallback).extend('com.tns.OnBackPressedCallback', {
handleOnBackPressed() {
if (Trace.isEnabled()) {
Trace.write('NativeScriptActivity.onBackPressed;', Trace.categories.NativeLifecycle);
}

const activity = this['_activity']?.get();

if (!activity) {
if (Trace.isEnabled()) {
Trace.write('NativeScriptActivity.onBackPressed; Activity is null, calling super', Trace.categories.NativeLifecycle);
}

this.setEnabled(false);

return;
}

const args = <AndroidActivityBackPressedEventData>{
eventName: 'activityBackPressed',

object: Application,

android: Application.android,

activity: activity,

cancel: false,
};

Application.android.notify(args);

if (args.cancel) {
return;
}

const view = activity._rootView;

let callSuper = false;

const viewArgs = <AndroidActivityBackPressedEventData>{
eventName: 'activityBackPressed',

object: view,

activity: activity,

cancel: false,
};

view?.notify(viewArgs);

// In the case of Frame, use this callback only if it was overridden, since the original will cause navigation issues

if (!viewArgs.cancel && (view?.onBackPressed === Frame.prototype.onBackPressed || !view?.onBackPressed())) {
callSuper = view instanceof Frame ? !Frame.goBack() : true;
}

if (callSuper) {
this.setEnabled(false);

activity.getOnBackPressedDispatcher().onBackPressed();

this.setEnabled(true);
}
},
});
}

const activityRootViewsMap = new Map<number, WeakRef<View>>();
const ROOT_VIEW_ID_EXTRA = 'com.tns.activity.rootViewId';

Expand Down Expand Up @@ -1055,4 +1128,13 @@ export class ActivityCallbacksImplementation implements AndroidActivityCallbacks

export function setActivityCallbacks(activity: androidx.appcompat.app.AppCompatActivity): void {
activity[CALLBACKS] = new ActivityCallbacksImplementation();
if (OnBackPressedCallback && !activity['_onBackPressed']) {
const callback = new OnBackPressedCallback(true);

callback['_activity'] = new WeakRef(activity);

activity['_onBackPressed'] = true;

(activity as androidx.activity.ComponentActivity).getOnBackPressedDispatcher().addCallback(activity, callback);
}
}
Loading