Android secondary display screen - persistence in calling activity

I use the Android MediaRouter (API 16) and Presentation (API 17) classes to create and manage an additional display. I followed the ApiDemos example to create a unique non-mirror output, and still works fine (testing with a Nexus 10 connected to an HDTV via an HDMI cable).

Now I have a situation where I want the Presentation object created in Activity X to continue to work on the secondary display, even if Activity X calls another Activity Y In this situation, Activity X is still on the stack, but Activity Y now on top of it.

The problem is that when Activity Y starts, the physical secondary display returns to mirroring. When I exit Activity Y , the contents of Activity X's Presentation is returned (since I never called dismiss() on it).

So the question is: How can I save a presentation attached to a secondary display running on that display, even if the sub-action is running on the local device?

UPDATE:. One of the ways I thought of doing this is to instantiate the Presentation object from the background thread; then the subsequent creation of another Activity should not interfere with the content that is written by the background stream to its Presentation object. But I'm not sure if this will work, as it is usually not allowed to update the user interface from a background thread.

Another approach would be to disable the use of secondary displays by the subordinate Activity , if possible, thereby preventing it from returning the secondary display to mirroring when the new Activity becomes active. But I also did not find a way to do this. Thanks again for any suggestions.

+4
source share
2 answers

I applied one of the approaches suggested by @CommonsWare (and regardless of Mark Ellison in answer to my question about his blog ). Thanks for your suggestions!

In the PROBLEM review, I could not save the second screen presentation, running in the background, through Activity invocations on the local device. This happened because the Presentation class is implemented as a subclass of Dialog and therefore attached to an Activity instance. Therefore, when a new Activity launched, the second screen returns to mirroring (instead of displaying other content that I specifically generated for it).

The DECISION was to reorganize all the "subordinate" Activities into Fragments original Activity (that is, the one that launched the second screen). Then, instead of calling startActivity() I start / stop the new Fragments using FragmentTransactions . The network effect is that the activity that started the presentation still works, so the secondary display is no longer interrupted when a new action is launched.

My business was complicated by the fact that the top level of Activity (which launches the second screen) was actually SherlockFragmentActivity , which uses ViewPager and FragmentStatePagerAdapter - so I had to ViewPager over all this in Fragment . It also required explicit management of ActionBar tabs, menu items, and the home icon.

All in all, I think the code is a little less transparent ... but it works!

NOTE. It’s good that Google has introduced an additional screen interface. But I'm not sure why they did it the way they did. It would be nice if they did not use the Presentation class in Presentation , if they provided a more general solution that could easily run in the background, i.e. Regardless of the foreground Activities on the device. This solution saved me a lot of code refactoring, as described above.

+2
source

Returning this question from the dead who want to help someone with the same problem somewhere in time,

Recently, I ran into a much deeper, but similar problem: I had to display the presentation anywhere in the system (I work with the built-in android), and any application can be used on the main screen.

At first, I thought about creating a service that controlled the presentation of the presentation and was initialized when the application started. But the problem was that I could not show the presentation, because, as you remember, it inherits from the dialog and the same problem that occurs when you call getApplicationContext () when creating the dialog.

My solution was: There is WindowManager.LayoutParam called TYPE_SYSTEM_ALERT , which is used to display alerts, such as the Low Battery Alert Dialog. Using this property, you can create a dialog from a service and display it correctly, and since the Presentation class is a child of the dialog box, just setting this property makes it work.

The magic happens here:

  WindowManager.LayoutParams l = mPresentation.getWindow() .getAttributes(); l.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; mPresentation.show(); 

Remember that in order to achieve this, your XML application must have SYSTEM_ALERT_WINDOW permission.

 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 

I think this should also solve your problem, but it is a little complicated and you need proper treatment to stop the presentation as soon as you need.

0
source

All Articles