0

I’m working on an Android app that has both Kotlin and Java activities. I want to handle screen orientation correctly depending on the device and the activity type. There are three main cases:

  1. Phone screens → always portrait.
  2. Tablet screens with most activities → can rotate freely.
  3. Tablet screens with restricted activities → cannot rotate; the activity stays in the orientation it was opened in (portrait or landscape).

As you can see in the code below, for "locked" activities I need to get the current orientation and then explicitly force the same orientation. Using requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LOCKED doesn’t seem to work as expected.

I’m a bit confused about what exactly should go into AndroidManifest.xml. I’ve tried using "android:screenOrientation="locked" and android:screenOrientation="unspecified", but neither seems to work reliably. Also, I’m not sure about the best moment to set the orientation — I’ve tried both before and right after super.onCreate(), but I still face issues. For example, when switching between activities, the new activity sometimes starts in portrait and then rotates to landscape (or just stays in portrait), even though the device was already in landscape and I called the "lock logic".

Is it possible that Kotlin and Java activities are handled differently in this regard? Or could the problem be related to navigating from a Kotlin activity to a Java activity (or vice versa)?

Here’s my Kotlin code for setting the orientation:

Kotlin code:

private fun setupOrientation(){
        if (!isTablet()) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        } else if (restrictedActivity){
            val currentOrientation = resources.configuration.orientation
            requestedOrientation = if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
                ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            } else {
                ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            }
        }
    }
3
  • 5
    Note that your scenarios 1 and 3 may no longer be supported by Android, once your targetSdk reaches 36 or higher, depending on your definition of "phone" and "tablet". "Is it possible that Kotlin and Java activities are handled differently in this regard?" -- no. "Or could the problem be related to navigating from a Kotlin activity to a Java activity (or vice versa)?" -- not just based on language. If the Java activities are legacy code, you might have other legacy bits that are impacting matters. Commented Oct 17 at 11:59
  • @CommonsWare yes I'm aware but right now my targetSdk = 35 and problems still appears. Commented Oct 17 at 12:19
  • My point is that your designers need to work on fixing their design limitations that are forcing you to do these orientation-based behaviors. Commented Oct 17 at 14:17

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.