yorkw
yorkw

Reputation: 41126

How to close Landscape VideoView Activity properly?

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

Upvotes: 9

Views: 5397

Answers (5)

FunkTheMonk
FunkTheMonk

Reputation: 10938

onConfigurationChanged will only be called if you add android:configChanges="orientation" to the activity in the manifest file.

This tells the system that you want to manually handle orientation changes, if you also have android:screenOrientation="landscape", you're also telling the system that the activity should be displayed in landscape mode (they should work together to give you what you want)

Upvotes: 0

Sherif elKhatib
Sherif elKhatib

Reputation: 45942

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
  }
}

Upvotes: 1

Joe
Joe

Reputation: 14059

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 :)

Upvotes: 17

Kibotu
Kibotu

Reputation: 191

You might use a completely different layout for landscape and portrait mode which I recommend, since you use can both differently.

I would recommend the API documentation to this too: http://developer.android.com/guide/topics/manifest/activity-element.html#screen (especially the part about handling savedBundle)

Upvotes: 0

user717572
user717572

Reputation: 3652

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.

Upvotes: 2

Related Questions