React-native-orientation: Event doesn't fire in Android

Created on 4 Aug 2017  路  21Comments  路  Source: yamill/react-native-orientation

Somehow, the Orientation.addOrientationListener event doesn't trigger in Android for me.
I've got a splashpage activity, so my configuration is slightly different than usual. What piece of code from the installation is responsible for making the event work? Or where can I look to get it working?

It works on IOS btw.

to-be-reviewed

Most helpful comment

As a workaround, I just figured that I can also get the orientation with the Dimensions API. Works just fine under android.

Dimensions.addEventListener('change', ({ window: { width, height } }) => {
    const orientation = width > height ? 'LANDSCAPE' : 'PORTRAIT';
}

All 21 comments

I am facing the same issue

Facing same issue.

Same

So this is not related to my setup.

I will try and investigate this issue soon. Sorry fellas been hella busy at work. Bear with me. If anyone would like to take a whack at it and submit a PR, even if it's WIP that'd be very helpful.

I had the same issue, but in my case i had created a new activity as the Main Activity and configured the other activity became the SplashActivity, the thing is that i wasn't setting the configChanges on the AndroidManifest.xml, when i set that, the event worked again.

      <activity
        android:hardwareAccelerated="true"
        android:name=".MainActivity"
          android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
          />

Is this fixed? I have android:configChanges and I also remove android:screenOrientation, because previously it was portrait. The orientation is actually changing, just that the event is not fired. Can we have the necessary code for AndroidManifest.xml in ReadMe so we can make sure everyone is on the right track.

Update: It turns out I skipped the "implement onConfigurationChanged method (in MainActivity.java)" part. Now everything is working.

Same issue on android simulator google nexus 5x 6.0.0 api 23
(onConfigurationChanged is implemented and library is linked properly)

@kelvinaliyanto does the event gets triggered in your case now plz let me now how you fixed it

I have the same issue. I'm using SplashActivity from react-native-navigation, not sure wether that has something to do with it, but it looks like SplashActivity is extending AppCompatActivity which has an onConfigurationChanged method, which looks like this:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    getDelegate().onConfigurationChanged(newConfig);
    if (mResources != null) {
        // The real (and thus managed) resources object was already updated
        // by ResourcesManager, so pull the current metrics from there.
        final DisplayMetrics newMetrics = super.getResources().getDisplayMetrics();
        mResources.updateConfiguration(newConfig, newMetrics);
    }
}

I have no experience with Android development, so I can鈥檛 really tell wether this is good enough for it to work.

@batusai513 are you also using react-native-navigation, how does your MainActivity look like?

As a workaround, I just figured that I can also get the orientation with the Dimensions API. Works just fine under android.

Dimensions.addEventListener('change', ({ window: { width, height } }) => {
    const orientation = width > height ? 'LANDSCAPE' : 'PORTRAIT';
}

Working fine, thanks for the workaround @MrLoh !

Yes. But orientation locking isn't working that way. Would still be greatly appreciated if someone could help.

I don't have any problem with orientation locking personnaly

I got receiving the events to work with React native navigation thanks to this thread: https://github.com/wix/react-native-navigation/pull/769#issuecomment-328150055

But I still can't lock the orientation.

Would be great to get a example code. Seems to work for me so just wondering what I did differently.

Im Setting the orientation lock in a redux actions side effect. Maybe it has to do with that, does the lock somehow resolve itself again? I doubt a code example would help. I call the function and the java function on the android side also gets called, so not sure how it could not work, unless I'm misunderstanding something about how it works. I though when I call the function the orientation gets locked until I call the unlock function.

Closing this since it was resolved in #231

@MrLoh Thank you very much!!!

Thank you very much!!! @MrLoh

Was this page helpful?
0 / 5 - 0 ratings