How to launch application on the second screen on Multi Display devices

1.8k views Asked by At

I am working on an Android application intended to run only on devices with two screens (running API 29). I have two somewhat related questions:

1. Is it possible to launch the application directly on the second display instead of primary display? Right now it launches on the primary display and I am then able to launch the subsequent activity on second display using ActivityOptions.

For example:

val exampleIntent = Intent(this, ExampleActivity::class.java)
exampleIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(
    exampleIntent, 
    ActivityOptions.makeBasic().setLaunchDisplayId(secondDisplayId).toBundle() 
)

2) Is it possible to explicitly finish an activity running on second display before relaunching it?

I have a usecase in which I need to relaunch an activity on the second display (from primary display) when some condition is met. However, if that activity is already running on the second screen then it is not relaunched.

Is there a way to finish/close that activity before launching it?

1

There are 1 answers

2
CommonsWare On

Is it possible to launch the application directly on the second display instead of primary display?

That is up to whoever is doing the launching. For example, if the launcher is doing the launching, the launcher would have to indicate that your app's entry point activity should go on the secondary display.

Is it possible to explicitly finish an activity running on second display before relaunching it?

That activity can call finish() to finish itself. You need to tell the activity to do that by one means or another, or the activity needs to know on its own that it is time to be finished. For example, the secondary-display activity can react to the same condition that you cite in the following sentence:

I have a usecase in which I need to relaunch an activity on the second display (from primary display) when some condition is met

To be honest, that feels like a coding limitation that you imposed yourself. From the operating system's standpoint, it is unclear what you are gaining by destroying and recreating the activity.

And, this behavior may look poor, depending on what sort of dual-screen environment you are using. For example, if the secondary display is a something like a TV, monitor, or projector, the default behavior of Android is to mirror the primary display on the secondary display. I would expect there to be substantial flicker with your proposed behavior:

  • You destroy the old secondary activity
  • Android starts mirroring the primary display on the secondary display
  • You start a replacement secondary activity

So, I would take great pains to avoid having to destroy and restart the activity. For example, perhaps the secondary display should not be displaying an activity, but rather a Presentation (which works more like a Dialog). Or, perhaps the secondary activity can simply update its own UI in response to your cited condition, rather than having to be rebuilt from scratch.