@@ -17,6 +17,7 @@ import { AndroidActivityBackPressedEventData, AndroidActivityNewIntentEventData,
1717import { Application } from '../../application/application' ;
1818import { isEmbedded , setEmbeddedView } from '../embedding' ;
1919import { CALLBACKS , FRAMEID , framesCache , setFragmentCallbacks } from './frame-helper-for-android' ;
20+ import { Device } from '../../platform' ;
2021
2122export * from './frame-common' ;
2223export { setFragmentClass } from './fragment' ;
@@ -762,6 +763,78 @@ function startActivity(activity: androidx.appcompat.app.AppCompatActivity, frame
762763 activity . startActivity ( intent ) ;
763764}
764765
766+ let OnBackPressedCallback ;
767+
768+ if ( parseInt ( Device . sdkVersion ) >= 33 ) {
769+ OnBackPressedCallback = ( < any > androidx . activity . OnBackPressedCallback ) . extend ( 'com.tns.OnBackPressedCallback' , {
770+ handleOnBackPressed ( ) {
771+ if ( Trace . isEnabled ( ) ) {
772+ Trace . write ( 'NativeScriptActivity.onBackPressed;' , Trace . categories . NativeLifecycle ) ;
773+ }
774+
775+ const activity = this [ '_activity' ] ?. get ( ) ;
776+
777+ if ( ! activity ) {
778+ if ( Trace . isEnabled ( ) ) {
779+ Trace . write ( 'NativeScriptActivity.onBackPressed; Activity is null, calling super' , Trace . categories . NativeLifecycle ) ;
780+ }
781+
782+ this . setEnabled ( false ) ;
783+
784+ return ;
785+ }
786+
787+ const args = < AndroidActivityBackPressedEventData > {
788+ eventName : 'activityBackPressed' ,
789+
790+ object : Application ,
791+
792+ android : Application . android ,
793+
794+ activity : activity ,
795+
796+ cancel : false ,
797+ } ;
798+
799+ Application . android . notify ( args ) ;
800+
801+ if ( args . cancel ) {
802+ return ;
803+ }
804+
805+ const view = activity . _rootView ;
806+
807+ let callSuper = false ;
808+
809+ const viewArgs = < AndroidActivityBackPressedEventData > {
810+ eventName : 'activityBackPressed' ,
811+
812+ object : view ,
813+
814+ activity : activity ,
815+
816+ cancel : false ,
817+ } ;
818+
819+ view ?. notify ( viewArgs ) ;
820+
821+ // In the case of Frame, use this callback only if it was overridden, since the original will cause navigation issues
822+
823+ if ( ! viewArgs . cancel && ( view ?. onBackPressed === Frame . prototype . onBackPressed || ! view ?. onBackPressed ( ) ) ) {
824+ callSuper = view instanceof Frame ? ! Frame . goBack ( ) : true ;
825+ }
826+
827+ if ( callSuper ) {
828+ this . setEnabled ( false ) ;
829+
830+ activity . getOnBackPressedDispatcher ( ) . onBackPressed ( ) ;
831+
832+ this . setEnabled ( true ) ;
833+ }
834+ } ,
835+ } ) ;
836+ }
837+
765838const activityRootViewsMap = new Map < number , WeakRef < View > > ( ) ;
766839const ROOT_VIEW_ID_EXTRA = 'com.tns.activity.rootViewId' ;
767840
@@ -1055,4 +1128,13 @@ export class ActivityCallbacksImplementation implements AndroidActivityCallbacks
10551128
10561129export function setActivityCallbacks ( activity : androidx . appcompat . app . AppCompatActivity ) : void {
10571130 activity [ CALLBACKS ] = new ActivityCallbacksImplementation ( ) ;
1131+ if ( OnBackPressedCallback && ! activity [ '_onBackPressed' ] ) {
1132+ const callback = new OnBackPressedCallback ( true ) ;
1133+
1134+ callback [ '_activity' ] = new WeakRef ( activity ) ;
1135+
1136+ activity [ '_onBackPressed' ] = true ;
1137+
1138+ ( activity as androidx . activity . ComponentActivity ) . getOnBackPressedDispatcher ( ) . addCallback ( activity , callback ) ;
1139+ }
10581140}
0 commit comments