Lavaraju
Lavaraju

Reputation: 2808

Orientation is not Updating After unlock the Device in react-native- android

i am Developing one Application, Actually my App is showing landscape and Portrait in Tabs/iPads. But in Tablets(iPad's working fine) when i was check the Orientation functionality working fine until unlock the Device.When i was lock the screen on Particular mode like (Portrait/Landscape) After that turned the Device showing before Orientation. Not Update the present Orientation.

I followed this link :https://github.com/yamill/react-native-orientation

this is my code:

componentWillMount(){
this.getOrientationtype()
}

getOrientationtype(){
    //alert("Hello")
    if(Platform.OS == 'ios'){  // to Identifying Android or iOS
     if(aspectRatio>1.6){  // Code for Iphone
       // alert("phone")
        Orientation.lockToPortrait()
       }
       else{
         Orientation.getOrientation((err, initial) => {
          if(initial != 'UNKNOWN'){
            this.setState({
              orientation:initial
            }) 
          }
          else{
            this.setState({
              orientation:'PORTRAIT'
            })
          }
        });

       }
     }
     else{

     if(DeviceInfo.isTablet()){
         // alert("android tab")
         Orientation.getOrientation((err, initial) => {
          if(initial != 'UNKNOWN'){
            this.setState({
              orientation:initial
            }) 
          }
          else{
            this.setState({
              orientation:'PORTRAIT'
            })
          }
        });
      }
      else{
        Orientation.lockToPortrait()
      }
     }
  }

Please find Out this Solution ....I am Using this link enter link description here

Upvotes: 4

Views: 1223

Answers (1)

ufxmeng
ufxmeng

Reputation: 2600

1.You should use Orientation.addOrientationListener for listen to the Orientation Events.

2.As see from the source code of OrientationModule.java, this library just call unregisterReceiver in onHostPause, so you can't receive onConfigurationChanged event after lock the screen.One way is to edit the onHostResume inside OrientationModule.java to meet what you want.

@Override
public void onHostResume() {
    final Activity activity = getCurrentActivity();

    if (activity == null) {
        FLog.e(ReactConstants.TAG, "no activity to register receiver");
        return;
    }
    activity.registerReceiver(receiver, new IntentFilter("onConfigurationChanged"));
    //add below code to onHostResume function
    //send broadcast onResume
    final int orientationInt = getReactApplicationContext().getResources().getConfiguration().orientation;
    Configuration newConfig = new Configuration();
    newConfig.orientation = orientationInt;
    Intent intent = new Intent("onConfigurationChanged");
    intent.putExtra("newConfig", newConfig);
    activity.sendBroadcast(intent);
}

the whole code can be found here OrientationModule.java

Upvotes: 1

Related Questions