Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dimensions.get('window') is not returning the current values on Android #29323

Closed
pmulholland42 opened this issue Jul 9, 2020 · 23 comments
Closed
Labels
API: Dimensions Needs: Triage 🔍 Platform: Android Android applications. Resolution: Locked This issue was locked by the bot.

Comments

@pmulholland42
Copy link

Description

The width and height returned by Dimensions.get('window') are not updating. The first time it's called, it returns the correct values. But if I change the device screen orientation and call it again, it returns the same values as before. This only happens on Android, and only started happening after I upgraded from RN 0.62.2 to 0.63.0

React Native version:

0.63.0
Android only

Steps To Reproduce

Provide a detailed list of steps that reproduce the issue.

  1. Add an onLayout like this to a View in your app: onLayout={() => { console.log(Dimensions.get('window')); }
  2. Rotate the device and notice that the width and height printed to the console stay the same after each rotation

Expected Results

The width and height should update to reflect the new screen layout

@JamesClarkeFP
Copy link

have you tried the newer useWindowDimensions()?

https://reactnative.dev/docs/usewindowdimensions

@pmulholland42
Copy link
Author

I haven't yet. Seems handy, but our app is all class components so we can't use it without rewriting them or wrapping them in function components.

@oliverdolgener
Copy link

oliverdolgener commented Jul 15, 2020

I can confirm. On Android the event listener won't trigger on orientation change. It makes no difference if we use Dimensions.addEventListener('change', setWindowSize); or useWindowDimensions(). On iOS it still works

@evelant
Copy link

evelant commented Jul 15, 2020

onLayout seems to have changed behavior in 0.63 but I'm not sure exactly how. It's breaking my app on iOS only right now by failing to call onLayout when a parent element resize causes a child element to move on the screen. Seems likely related to this issue. What changed with onLayout in 0.63?

@aryojamousi
Copy link

aryojamousi commented Jul 16, 2020

weirdly, when using useDimensions in this package the device orientation changes get detected...

@NguyenKyThinh94
Copy link

I can confirm. On Android the event listener won't trigger on orientation change. It makes no difference if we use Dimensions.addEventListener('change', setWindowSize); or useWindowDimensions(). On iOS it still works

Yes, it not work with android !!

@R4DIC4L
Copy link

R4DIC4L commented Oct 8, 2020

I can confirm it is not working on android, but on iOS is it still ok. Using RN 0.63.2. Probably also happening on 0.63.3, I will try to update later.

This has broken my orientation-dependent rendering, I had to check the 'screen' dimensions to get the correct orientation change but I see a lag in the orientation layout change after device rotation of about 2-3 seconds (only on android).

@wmonecke

This comment has been minimized.

@evelant
Copy link

evelant commented Oct 15, 2020

This one is pretty mild, it has only been open for 3 months. There are outstanding bugs going on 4 years without solutions. There's a lot of undocumented, inconsistent-across-platforms, or (especially on Android) broken stuff in RN. FB has developed somewhat of a reputation for subpar community management with this project. Many issues get closed without resolution or explanation. PRs linger for months or years. Long standing bugs never get fixed/merged because FB has closed internal workarounds. etc, etc.

I'm not complaining however, it's free software, we can fork and fix it ourselves if the quirks bother us. It's just a shame to see an innovative and otherwise great project like RN marred and slowed by seemingly indifferent maintainers.

As for a fix, I've found that many measurement problems on Android are corrected by doing two things:

  1. Add collapsible={false} to your views you plan to measure. Even if you have a ref on a view Android/RN will still "optimize" (remove) it leading to unexpected behavior
  2. Use a ref and the .measure function to get the measurement at the time you need it instead of relying on onLayout. Frequently onLayout doesn't get called when expected or doesn't return the values you expect because of animations or mounting behavior. Just be aware that .measure returns different values than onLayout. IIRC measure will give you the absolute pixel size on screen where onLayout gives you the untransformed original size (ex: if transform: [{scale: 2}] is on a view or any of it's parents onLayout will give width: 50 and .measure will give width: 100)

@oleksandr-dziuban
Copy link

@react-native-bot This is the really critical issue which breaks all Android orientation related styling in apps. When this can be reviewed by maintainers? Thanks...

facebook-github-bot pushed a commit that referenced this issue Nov 11, 2020
Summary:
Currently the dimensions are created once, and then cached.  This change will reload the dimensions when the device orientation changes to insure that dimension update events follow orientation changed events.

this should help address the following issues, that I know of:
#29105
#29451
#29323

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[Android] [Fixed] - Dimension update events are now properly sent following orientation change

Pull Request resolved: #30324

Test Plan: Open up RNTester app.  Select the Dimensions API list item.  Rotate the device and verify that the dimensions are correct based on orientation.

Reviewed By: fkgozali

Differential Revision: D24874733

Pulled By: ejanzer

fbshipit-source-id: 867681ecb009d368a2ae7b67d94d6355e67dea7b
@luisnaranjo733
Copy link

luisnaranjo733 commented Nov 11, 2020

The fix for this is checked into the master branch (thank you @ajpaulingalls and @ejanzer) and will naturally make its way into the first react-native@0.64 release candidates.
If you want it to be included in the future react-native@0.63.4 patch release, please upvote the comment linked below.

react-native-community/releases#212 (comment)

alloy pushed a commit to alloy/react-native that referenced this issue Nov 19, 2020
Summary:
Currently the dimensions are created once, and then cached.  This change will reload the dimensions when the device orientation changes to insure that dimension update events follow orientation changed events.

this should help address the following issues, that I know of:
facebook#29105
facebook#29451
facebook#29323

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[Android] [Fixed] - Dimension update events are now properly sent following orientation change

Pull Request resolved: facebook#30324

Test Plan: Open up RNTester app.  Select the Dimensions API list item.  Rotate the device and verify that the dimensions are correct based on orientation.

Reviewed By: fkgozali

Differential Revision: D24874733

Pulled By: ejanzer

fbshipit-source-id: 867681ecb009d368a2ae7b67d94d6355e67dea7b
kelset pushed a commit that referenced this issue Nov 27, 2020
Summary:
Currently the dimensions are created once, and then cached.  This change will reload the dimensions when the device orientation changes to insure that dimension update events follow orientation changed events.

this should help address the following issues, that I know of:
#29105
#29451
#29323

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[Android] [Fixed] - Dimension update events are now properly sent following orientation change

Pull Request resolved: #30324

Test Plan: Open up RNTester app.  Select the Dimensions API list item.  Rotate the device and verify that the dimensions are correct based on orientation.

Reviewed By: fkgozali

Differential Revision: D24874733

Pulled By: ejanzer

fbshipit-source-id: 867681ecb009d368a2ae7b67d94d6355e67dea7b
brentvatne pushed a commit to expo/react-native that referenced this issue Dec 3, 2020
Summary:
Currently the dimensions are created once, and then cached.  This change will reload the dimensions when the device orientation changes to insure that dimension update events follow orientation changed events.

this should help address the following issues, that I know of:
facebook#29105
facebook#29451
facebook#29323

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[Android] [Fixed] - Dimension update events are now properly sent following orientation change

Pull Request resolved: facebook#30324

Test Plan: Open up RNTester app.  Select the Dimensions API list item.  Rotate the device and verify that the dimensions are correct based on orientation.

Reviewed By: fkgozali

Differential Revision: D24874733

Pulled By: ejanzer

fbshipit-source-id: 867681ecb009d368a2ae7b67d94d6355e67dea7b
@bigcupcoffee
Copy link
Contributor

bigcupcoffee commented Dec 24, 2020

I'd like to note that my issue about the dimensions API is still actual, and dimensions is not working good with (at least) a split-screen feature, my issue is #30371 and it's still broken on 0.63.4(as I just tested), since the patch mentioned above was included in 0.63.4 I expected it to work, but it didn't do a thing to me.

Also let me know if I should close my issue(react with 👀), since this one seems to be the same. Can anyone else also confirm the issue being present in 0.63.4, or am I trippin

@martingg88
Copy link

any solution for this issue?

@jcabrerazuniga
Copy link

jcabrerazuniga commented Mar 16, 2021

Hi
I am in this problem, too... any solution? With this bug React Native is nearly unusable. (Using RN 0.64). This problem also happens if using React Native Functional Components.

@bigcupcoffee
Copy link
Contributor

@martingg88 @jcabrerazuniga #30371 (comment) might be pretty helpful for certain cases

@ws333
Copy link

ws333 commented May 3, 2021

Dimensions.get('screen') still doen'tt update when rotating device. I have to close/reopen the app to get it to update.
Dimensions.get('window') seems to work though, but I use both in my app to calculate dim of statusbar/navbar.

RN version 0.64

@0rsa
Copy link

0rsa commented May 5, 2021

Hello,
I can't use orientation feature. useWindowDimensions() never fires or a console.log("dimensions") forcing a rerender is never updated with right values.
My app is already big and its is complicated to know what it can be conflicting
0.64.0 / Android 11

savv pushed a commit to savv/react-native-savv that referenced this issue May 18, 2021
Summary:
Currently the dimensions are created once, and then cached.  This change will reload the dimensions when the device orientation changes to insure that dimension update events follow orientation changed events.

this should help address the following issues, that I know of:
facebook#29105
facebook#29451
facebook#29323

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[Android] [Fixed] - Dimension update events are now properly sent following orientation change

Pull Request resolved: facebook#30324

Test Plan: Open up RNTester app.  Select the Dimensions API list item.  Rotate the device and verify that the dimensions are correct based on orientation.

Reviewed By: fkgozali

Differential Revision: D24874733

Pulled By: ejanzer

fbshipit-source-id: 867681ecb009d368a2ae7b67d94d6355e67dea7b
(cherry picked from commit 0e9296b)
luisnaranjo733 added a commit to luisnaranjo733/react-native that referenced this issue May 25, 2021
Summary:
Currently the dimensions are created once, and then cached.  This change will reload the dimensions when the device orientation changes to insure that dimension update events follow orientation changed events.

this should help address the following issues, that I know of:
facebook#29105
facebook#29451
facebook#29323

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[Android] [Fixed] - Dimension update events are now properly sent following orientation change

Pull Request resolved: facebook#30324

Test Plan: Open up RNTester app.  Select the Dimensions API list item.  Rotate the device and verify that the dimensions are correct based on orientation.

Reviewed By: fkgozali

Differential Revision: D24874733

Pulled By: ejanzer

fbshipit-source-id: 867681ecb009d368a2ae7b67d94d6355e67dea7b
kelset pushed a commit that referenced this issue May 26, 2021
Summary:
Currently the dimensions are created once, and then cached.  This change will reload the dimensions when the device orientation changes to insure that dimension update events follow orientation changed events.

this should help address the following issues, that I know of:
#29105
#29451
#29323

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[Android] [Fixed] - Dimension update events are now properly sent following orientation change

Pull Request resolved: #30324

Test Plan: Open up RNTester app.  Select the Dimensions API list item.  Rotate the device and verify that the dimensions are correct based on orientation.

Reviewed By: fkgozali

Differential Revision: D24874733

Pulled By: ejanzer

fbshipit-source-id: 867681ecb009d368a2ae7b67d94d6355e67dea7b
@jmsbrett
Copy link

This is still an issue in 64.1.

@ws333
Copy link

ws333 commented Jun 11, 2021

This is still an issue in 64.1.

@jmsbrett Version 0.64.2 seems to have a fix for it.

@bigcupcoffee
Copy link
Contributor

bigcupcoffee commented Jun 11, 2021

@ws333 just tested, 0.64.2 still got #30371 though, going to bump it once again at 0.65 😩

@MrAdex77
Copy link

MrAdex77 commented Jul 2, 2021

Hi, is there any fix? still its not working for me on android
@edit
Updated to 0.64.2 and now its finally working.. uff

facebook-github-bot pushed a commit that referenced this issue Aug 18, 2021
Summary:
When retrieving the device dimensions through the JS `Dimensions` utility, the result of `Dimensions.get` can be incorrect on Android.

### Related issues

- #29105
- #29451
- #29323

The issue is caused by the Android `DeviceInfoModule` that provides initial screen dimensions and then subsequently updates those by emitting `didUpdateDimensions` events. The assumption in that implementation is that the initial display metrics will not have changed prior to the first check for updated metrics. However that is not the case as the device may be rotated (as shown in the attached video).

The solution in this PR is to keep track of the initial dimensions for comparison at the first check for updated metrics.

## Changelog

[Android] [Fixed] - Fix Dimensions not updating

Pull Request resolved: #31973

Test Plan:
### Steps to reproduce
1. Install the RNTester app on Android from the `main` branch.
2. Set the device auto-rotation to ON
3. Start the RNTester app
4. While the app is loading, rotate the device
5. Navigate to the `Dimensions` screen
6. Either
 a. Observe the screen width and height are reversed, or
 b. Quit the app and return to step 3.

### Verifying the fix

#### Manually
Using the above steps, the issue should no longer be reproducible.

#### Automatically
See unit tests in `ReactAndroid/src/test/java/com/facebook/react/modules/deviceinfo/DeviceInfoModuleTest.java`

### Video

https://user-images.githubusercontent.com/4940864/128485453-2ae04724-4ac5-4267-a59a-140cc3af626b.mp4

Reviewed By: JoshuaGross

Differential Revision: D30319919

Pulled By: lunaleaps

fbshipit-source-id: 52a2faeafc522b1c2a196ca40357027eafa1a84b
mahi8813 pushed a commit to myntra/react-native that referenced this issue Aug 31, 2021
Summary:
When retrieving the device dimensions through the JS `Dimensions` utility, the result of `Dimensions.get` can be incorrect on Android.

- facebook#29105
- facebook#29451
- facebook#29323

The issue is caused by the Android `DeviceInfoModule` that provides initial screen dimensions and then subsequently updates those by emitting `didUpdateDimensions` events. The assumption in that implementation is that the initial display metrics will not have changed prior to the first check for updated metrics. However that is not the case as the device may be rotated (as shown in the attached video).

The solution in this PR is to keep track of the initial dimensions for comparison at the first check for updated metrics.

[Android] [Fixed] - Fix Dimensions not updating

Pull Request resolved: facebook#31973

Test Plan:
1. Install the RNTester app on Android from the `main` branch.
2. Set the device auto-rotation to ON
3. Start the RNTester app
4. While the app is loading, rotate the device
5. Navigate to the `Dimensions` screen
6. Either
 a. Observe the screen width and height are reversed, or
 b. Quit the app and return to step 3.

Using the above steps, the issue should no longer be reproducible.

See unit tests in `ReactAndroid/src/test/java/com/facebook/react/modules/deviceinfo/DeviceInfoModuleTest.java`

https://user-images.githubusercontent.com/4940864/128485453-2ae04724-4ac5-4267-a59a-140cc3af626b.mp4

Reviewed By: JoshuaGross

Differential Revision: D30319919

Pulled By: lunaleaps

fbshipit-source-id: 52a2faeafc522b1c2a196ca40357027eafa1a84b
mahi8813 pushed a commit to myntra/react-native that referenced this issue Sep 1, 2021
Summary:
When retrieving the device dimensions through the JS `Dimensions` utility, the result of `Dimensions.get` can be incorrect on Android.

- facebook#29105
- facebook#29451
- facebook#29323

The issue is caused by the Android `DeviceInfoModule` that provides initial screen dimensions and then subsequently updates those by emitting `didUpdateDimensions` events. The assumption in that implementation is that the initial display metrics will not have changed prior to the first check for updated metrics. However that is not the case as the device may be rotated (as shown in the attached video).

The solution in this PR is to keep track of the initial dimensions for comparison at the first check for updated metrics.

[Android] [Fixed] - Fix Dimensions not updating

Pull Request resolved: facebook#31973

Test Plan:
1. Install the RNTester app on Android from the `main` branch.
2. Set the device auto-rotation to ON
3. Start the RNTester app
4. While the app is loading, rotate the device
5. Navigate to the `Dimensions` screen
6. Either
 a. Observe the screen width and height are reversed, or
 b. Quit the app and return to step 3.

Using the above steps, the issue should no longer be reproducible.

See unit tests in `ReactAndroid/src/test/java/com/facebook/react/modules/deviceinfo/DeviceInfoModuleTest.java`

https://user-images.githubusercontent.com/4940864/128485453-2ae04724-4ac5-4267-a59a-140cc3af626b.mp4

Reviewed By: JoshuaGross

Differential Revision: D30319919

Pulled By: lunaleaps

fbshipit-source-id: 52a2faeafc522b1c2a196ca40357027eafa1a84b
@nickfujita
Copy link
Contributor

Updated to 0.63.4 from 0.63.3, and confirmed fixed on Android.

@kelset kelset closed this as completed Sep 27, 2021
@lastant
Copy link

lastant commented Oct 26, 2021

My problem on 0.61.5 was not that the values were not correct, but that the view was not re-rendered at all after the rotation, so I added this to the constructor() and it worked:

    Dimensions.addEventListener('change', () => {
      this.setState({});
    });

@facebook facebook locked as resolved and limited conversation to collaborators Sep 27, 2022
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Sep 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
API: Dimensions Needs: Triage 🔍 Platform: Android Android applications. Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests