@@ -17,7 +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 ' ;
20+ import { SDK_VERSION } from '../../utils ' ;
2121
2222export * from './frame-common' ;
2323export { setFragmentClass } from './fragment' ;
@@ -765,34 +765,28 @@ function startActivity(activity: androidx.appcompat.app.AppCompatActivity, frame
765765
766766let OnBackPressedCallback ;
767767
768- if ( parseInt ( Device . sdkVersion ) >= 33 ) {
768+ if ( SDK_VERSION >= 33 ) {
769769 OnBackPressedCallback = ( < any > androidx . activity . OnBackPressedCallback ) . extend ( 'com.tns.OnBackPressedCallback' , {
770770 handleOnBackPressed ( ) {
771771 if ( Trace . isEnabled ( ) ) {
772772 Trace . write ( 'NativeScriptActivity.onBackPressed;' , Trace . categories . NativeLifecycle ) ;
773773 }
774774
775- const activity = this [ '_activity' ] ?. get ( ) ;
776-
775+ const activity = this [ '_activity' ] ?. get ( ) as androidx . appcompat . app . AppCompatActivity ;
777776 if ( ! activity ) {
778777 if ( Trace . isEnabled ( ) ) {
779778 Trace . write ( 'NativeScriptActivity.onBackPressed; Activity is null, calling super' , Trace . categories . NativeLifecycle ) ;
780779 }
781780
782781 this . setEnabled ( false ) ;
783-
784782 return ;
785783 }
786784
787785 const args = < AndroidActivityBackPressedEventData > {
788786 eventName : 'activityBackPressed' ,
789-
790787 object : Application ,
791-
792788 android : Application . android ,
793-
794789 activity : activity ,
795-
796790 cancel : false ,
797791 } ;
798792
@@ -802,33 +796,32 @@ if (parseInt(Device.sdkVersion) >= 33) {
802796 return ;
803797 }
804798
805- const view = activity . _rootView ;
806-
807- let callSuper = false ;
799+ const callbacks : AndroidActivityCallbacks = activity [ CALLBACKS ] ;
800+ let callSuper : boolean = false ;
808801
809- const viewArgs = < AndroidActivityBackPressedEventData > {
810- eventName : 'activityBackPressed' ,
802+ if ( callbacks ) {
803+ const view = callbacks . getRootView ( ) ;
811804
812- object : view ,
805+ if ( view ) {
806+ const viewArgs = < AndroidActivityBackPressedEventData > {
807+ eventName : 'activityBackPressed' ,
808+ object : view ,
809+ activity : activity ,
810+ cancel : false ,
811+ } ;
813812
814- activity : activity ,
813+ view . notify ( viewArgs ) ;
815814
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 ;
815+ // In the case of Frame, use this callback only if it was overridden, since the original will cause navigation issues
816+ if ( ! viewArgs . cancel && ( view . onBackPressed === Frame . prototype . onBackPressed || ! view . onBackPressed ( ) ) ) {
817+ callSuper = view instanceof Frame ? ! Frame . goBack ( ) : true ;
818+ }
819+ }
825820 }
826821
827822 if ( callSuper ) {
828823 this . setEnabled ( false ) ;
829-
830824 activity . getOnBackPressedDispatcher ( ) . onBackPressed ( ) ;
831-
832825 this . setEnabled ( true ) ;
833826 }
834827 } ,
0 commit comments