In my application, I have an activity play http live streaming video in landscape mode.
My AndroidManifest.xml:
<activity android:name=".MediaPlayerActivity"
android:label="@string/menu_player"
android:launchMode="singleInstance"
android:screenOrientation="landscape">
</activity>
My activity layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black">
<VideoView android:id="@+id/myVideoView"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
</LinearLayout>
The problem is everytime I close this activity (By clicking back button), it always rotate to portrait mode (time is very soon but you can actually see the effect on real device before back to the previous activity) before closing. How can I fix this annoying issue?
Update with more info
This annoying behaviour only happened If the previous activity is in portrait mode, if the previous one is landscape, it is just fine. To me it looks like something to do with Android framework when fade in/out activities with different screenOrientation settings.
Update with the cause
After some deep reading through Google's API, I think I found the reason that cause this annoying behaviour, check out here:
Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause(), onStop(), and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle).
So what happened behind the scenes when back button is clicked: currnet VideoView Activity (landscape) is destroyed, a new VideoView Activity (portrait) is created due screenOrientation configuration has been changed, and destoryed immidiately (where you can see the effects on screen), last activity in stack is shown. this also explain why this annoying behaviour disappered if last activity has the same screenOrientation settngs.
I am still trying to figure out how to bypass this activity recreation due to configuration change. As it stated in API, overriding onConfigurationChanged(Configuration), however, since I explicitly define screenOrientation in xml, onConfigurationChanged() is not called, lots of similiar SO has been discussed before, like this one.
Please provide answer on the right direction.
Thanks,
Y
Try adding calls to VideoView's suspend()
, resume()
and stopPlayback()
in your activity's onPause()
, onResume()
and onDestroy()
methods:
@Override
protected void onResume() {
mVideoView.resume();
super.onResume();
}
@Override
protected void onPause() {
mVideoView.suspend();
super.onPause();
}
@Override
protected void onDestroy() {
mVideoView.stopPlayback();
super.onDestroy();
}
The VideoView class implementation varies from one device to another (and the class itself is very sparsely documented), but the Gallery3D code from AOSP does call the above methods in its MovieView activity lifecycle methods, so hopefully most devices should at least make itself looks good in that scenario.
If it still looks bad, you might want to override onBackPressed()
in your activity to maybe hide the VideoView or some similar hacks to conceal the annoying behavior :)
Since your activity isn't always recreated if you close one, It may not execute your manifest code always, tough i'm not sure of this.
But something you could try is setting the orientation manually in onResume:
@Override
protected void onResume()
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
super.onResume();
}
this has more on it.
I beleive that a simple solution would to add a field inside your video activity:
boolean isClosing = false;
Set isClosing
to true
in the onDestroy
or, much better, in the onBackPressed
(before calling finish()
)
Now, I supposed you have set onConfigurationChanged
to catch orientation changes.
So, in your onConfigurationChanged
Encapsulate it with if(!isClosing)
@Override
public void onConfigurationChanged(Configuration newConfig)
{
if(!isClosing){
//YOUR OLD CODE
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With