-1

Does anybody know how to cope the problem? Using the android.app.Activity#setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE) method almost always causes the showing a black screen for a split second during the rotation.

But if I rotate the smartphone itself, then the black screen never appears. How to get rid of the black screen? Is it the Android issue of the setRequestedOrientation() method, or I do something wrong?

EDIT: AFAIK the black screen with setRequestedOrientation() call could happen if we have quite heavy layout, when it takes some more time to initialize it in the UI thread. If you try to call setRequestedOrientation() for instance in a "Hello World!" app with a light layout, you won't see the black screen. So, the loading on the UI thread can be the cause of the black screen when you rotate with setRequestedOrientation(). But, in other hand why it never appears when I rotate the screen changing the phone orientation?

Anton Kaliturin
  • 147
  • 1
  • 6

2 Answers2

2

The only solution I've found. Consider I want to turn screen to landscape and what do I do:

  1. I remove the root Fragment with heavy stuff from the Activity I want to rotate.
  2. Inside the Activity I want to rotate I open an empty white Activity for a moment to cover the black screen. The Activity is calling setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE) in its onCreate callback and closes itself after the timeout = 500 ms. I open/close the Activity with fade-in/fade-out animation.
  3. Inside the Activity I want to rotate I call setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE) post delayed with 100 ms. This starts the rotation.
  4. After the rotation complete, in the onCreate I add the removed Fragment back.

That's it. Rotation to landscape is finished and the black screen was completely covered.

Anton Kaliturin
  • 147
  • 1
  • 6
  • Thanks. After create a emptyActivity, It works perfect.I am not able to see any black screen. Save my day – Priyanka Jun 01 '23 at 23:18
0

Edited:

I had the same problem. In older versions of android, if the content of the layout is heavy (if it contains high resolution image, etc.), the running activity is trying to re-create the layout to change the orientation but can't achieve this.

As a solution, we can create 2 different layout resources as portrait and landscape, specifying which one we will use and making it easier for the android.

Talha Ç
  • 74
  • 1
  • 8