I am using google's sample project on Media Projection API. what I observe is that every time screen orientation changes the VirtualDisplay
object returned as
mVirtualDisplay = mMediaProjection.createVirtualDisplay("ScreenCapture",
mSurfaceView.getWidth(), mSurfaceView.getHeight(), mScreenDensity,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mSurface, null, null);
is destroyed (set to null
) and I have to restart the screen capture. I understand that this is perhaps required due to change of screen dimensions, but I was hoping there's a away to avoid destroying this object and merely updating it so the screen capture only stops when stop button is pressed. Obviously one can restart the screen capture programmatically after orientation change, but I'd rather not do that. Am I being too optimistic here?
So the problem was with android's default behavior when configuration changes. It basically destroys the activity and all of its fields (in this case including VirtualDisplay instance). Also, retaining the VirtualDisplay instance as a UI state here is not an option. so I ended up handling the orientation change myself as described here by adding configChanges
tag to my activity
<activity
android:name=".MyActivity"
android:configChanges="orientation|screenSize|keyboardHidden" />
This means nothing is destroyed on orientation change anymore but layout changes need to be handled manually using activity's onConfigurationChanged
callback.