简体   繁体   English

在方向更改中保留xml片段

[英]retain xml fragment across orientation changes

I am using the <fragment> tag to add a Fragment to an Activity. 我正在使用<fragment>标记将片段添加到活动。 The fragment contains a TextureView that is playing a video. 该片段包含正在播放视频的TextureView。 How do I keep the video playing on orientation changes? 如何保持视频在方向变化时播放?

FYI: My fragment has other stuff in it such as Service and Broadcast receiver, so I am looking for a solid engineering solution. 仅供参考:我的片段中还包含其他内容,例如服务和广播接收器,因此我正在寻找可靠的工程解决方案。 For instance, if I use 例如,如果我使用

@Override
public void onCreate(Bundle savedInstanceState){
  setRetainInstance(true);
}

It may cause my service to leak on orientation change. 这可能会导致我的服务因方向改变而泄漏。

I am reading the fragments document and am looking at answers here as well. 我正在阅读碎片文档,也在这里查看答案。 But fragments are such complicated features that I need a bit extra help. 但是片段是如此复杂,我需要一些额外的帮助。

In your activity manifest 在您的活动清单中

android:configChanges="orientation"
android:screenOrientation="portrait"

then override onConfigurationChanged 然后覆盖onConfigurationChanged

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

Have you tied this: 你绑了这个:

android:configChanges="orientation|keyboardHidden"

or for Android 3.2 (API level 13) and newer: 或适用于Android 3.2(API级别13)及更高版本:

android:configChanges="orientation|keyboardHidden|screenSize"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM