@@ -42,6 +42,7 @@ export class AndroidApplication extends Observable implements AndroidApplication
4242 public static activityNewIntentEvent = ActivityNewIntent ;
4343 public static activityRequestPermissionsEvent = ActivityRequestPermissions ;
4444
45+ private _orientation : "portrait" | "landscape" | "unknown" ;
4546 public paused : boolean ;
4647 public nativeApp : android . app . Application ;
4748 public context : android . content . Context ;
@@ -80,6 +81,22 @@ export class AndroidApplication extends Observable implements AndroidApplication
8081 this . _pendingReceiverRegistrations . length = 0 ;
8182 }
8283
84+ get orientation ( ) : "portrait" | "landscape" | "unknown" {
85+ if ( ! this . _orientation ) {
86+ const resources = this . context . getResources ( ) ;
87+ const configuration = < android . content . res . Configuration > resources . getConfiguration ( ) ;
88+ const orientation = configuration . orientation ;
89+
90+ this . _orientation = getOrientationValue ( orientation ) ;
91+ }
92+
93+ return this . _orientation ;
94+ }
95+
96+ set orientation ( value : "portrait" | "landscape" | "unknown" ) {
97+ this . _orientation = value ;
98+ }
99+
83100 public registerBroadcastReceiver ( intentFilter : string , onReceiveCallback : ( context : android . content . Context , intent : android . content . Intent ) => void ) {
84101 ensureBroadCastReceiverClass ( ) ;
85102 const that = this ;
@@ -232,6 +249,17 @@ global.__onLiveSync = function __onLiveSync(context?: ModuleContext) {
232249 livesync ( rootView , context ) ;
233250} ;
234251
252+ function getOrientationValue ( orientation : number ) : "portrait" | "landscape" | "unknown" {
253+ switch ( orientation ) {
254+ case android . content . res . Configuration . ORIENTATION_LANDSCAPE :
255+ return "landscape" ;
256+ case android . content . res . Configuration . ORIENTATION_PORTRAIT :
257+ return "portrait" ;
258+ default :
259+ return "unknown" ;
260+ }
261+ }
262+
235263function initLifecycleCallbacks ( ) {
236264 const setThemeOnLaunch = profile ( "setThemeOnLaunch" , ( activity : androidx . appcompat . app . AppCompatActivity ) => {
237265 // Set app theme after launch screen was used during startup
@@ -321,7 +349,6 @@ function initLifecycleCallbacks() {
321349 return lifecycleCallbacks ;
322350}
323351
324- let currentOrientation : number ;
325352function initComponentCallbacks ( ) {
326353 let componentCallbacks = new android . content . ComponentCallbacks2 ( {
327354 onLowMemory : profile ( "onLowMemory" , function ( ) {
@@ -335,32 +362,19 @@ function initComponentCallbacks() {
335362 } ) ,
336363
337364 onConfigurationChanged : profile ( "onConfigurationChanged" , function ( newConfig : android . content . res . Configuration ) {
338- const newOrientation = newConfig . orientation ;
339- if ( newOrientation === currentOrientation ) {
340- return ;
365+ const newConfigOrientation = newConfig . orientation ;
366+ const newOrientation = getOrientationValue ( newConfigOrientation ) ;
367+
368+ if ( androidApp . orientation !== newOrientation ) {
369+ androidApp . orientation = newOrientation ;
370+
371+ notify ( < OrientationChangedEventData > {
372+ eventName : orientationChangedEvent ,
373+ android : androidApp . nativeApp ,
374+ newValue : androidApp . orientation ,
375+ object : androidApp
376+ } ) ;
341377 }
342-
343- currentOrientation = newOrientation ;
344- let newValue ;
345-
346- switch ( newOrientation ) {
347- case android . content . res . Configuration . ORIENTATION_LANDSCAPE :
348- newValue = "landscape" ;
349- break ;
350- case android . content . res . Configuration . ORIENTATION_PORTRAIT :
351- newValue = "portrait" ;
352- break ;
353- default :
354- newValue = "unknown" ;
355- break ;
356- }
357-
358- notify ( < OrientationChangedEventData > {
359- eventName : orientationChangedEvent ,
360- android : androidApp . nativeApp ,
361- newValue : newValue ,
362- object : androidApp
363- } ) ;
364378 } )
365379 } ) ;
366380
@@ -399,4 +413,4 @@ declare namespace com {
399413 static getInstance ( ) : NativeScriptApplication ;
400414 }
401415 }
402- }
416+ }
0 commit comments