方向改变后如何保留片段播放视频片段的实例? [英] How to retain instance of fragment of fragment playing video after change in orientation?

查看:90
本文介绍了方向改变后如何保留片段播放视频片段的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个YouTube API片段,该片段是静态添加到我的xml清单文件中的,即其中包含youtube播放器的片段.

I have a YouTube API fragment that is statically added in my xml manifest file, i.e. a fragment that has a youtube player inside of it.

我的项目中没有扩展片段的文件.

I do not have a file that extends fragment in my project.

在活动类中,将以下代码行放入活动类的onCreate中:

In my activity class I put this line of code in the onCreate of my activity class:

youTubePlayerFragment.setRetainInstance(true);

它没有任何影响:旋转屏幕时,我得到了一个空白的黑色屏幕片段.

It does not have any affect: when I rotate the screen I get a blank black screen of the fragment.

如何更改方向后继续播放片段?

How can I get it to continue playing the fragment after the orientation change?

<fragment
      android:id="@+id/youtube_fragment"
      android:name="com.google.android.youtube.player.YouTubePlayerFragment"
      android:layout_width="900dp"
      android:layout_height="500dp"
      android:layout_centerHorizontal="true"
      android:layout_centerVertical="true" />

推荐答案

快速简便的解决方案是在清单中的视频活动中添加以下内容:

The quick and easy solution is to add the following to your video activity, in your manifest:

android:configChanges="orientation|keyboard|keyboardHidden|screenSize"

我有一个应用程序将此视频用于视频,并且效果很好.一切都随着方向改变而正确地布置,并且活动和片段实例也不会被拆除,从而可以无缝地继续播放.

I have an app that uses this for a video and it works just fine. Everything lays out properly on orientation change, and the activity and fragment instance do not get torn down allowing it to seamlessly continue to play.

如果您希望拆除/重新创建活动,同时保留片段实例,请重新阅读setRetainInstance()的文档.为了使它正常工作,您需要注意一些细微差别: http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)

If you want the activity to be torn down/recreated, while retaining the fragment instance, please re-read the documentation for setRetainInstance(). There are some subtle nuances that you need to be aware of to get this to work properly: http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)

这篇关于方向改变后如何保留片段播放视频片段的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆