Home » Android » android – How Can We Get an Activity Shown Using setLaunchDisplayId() to be Landscape?

android – How Can We Get an Activity Shown Using setLaunchDisplayId() to be Landscape?

Posted by: admin May 14, 2020 Leave a comment

Questions:

In this sample project, my MainActivity has code to launch a separate activity on an external display, using setLaunchDisplayId() on ActivityOptions:

class MainActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    getSystemService(DisplayManager::class.java)
      .getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION)
      .firstOrNull()
      ?.let { display ->
        Intent(this, ExternalDisplayActivity::class.java)
          .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
          .let { intent ->
            ActivityOptions.makeBasic()
              .setLaunchDisplayId(display.displayId)
              .toBundle()
              .let { opts ->
                startActivity(intent, opts)
              }
          }
      }
  }
}

ExternalDisplayActivity launches fine onto the external display. However; it shows up in portrait mode, not landscape. This occurs both using a real monitor and a simulated external display from Developer Options in Settings.

Strangely enough, this comes despite the fact that I have android:screenOrientation="landscape" on the ExternalDisplayActivity manifest entry and I request landscape at runtime:

class ExternalDisplayActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_external)
    requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
  }
}

landscape works if I use a Presentation and that older API, but I cannot figure out how to get landscape working with the setLaunchDisplayId() route.

How to&Answers: