片段保留不保留 [英] Retained Fragment Not Retained

查看:149
本文介绍了片段保留不保留的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含VideoView一个简单的布局。

I have a simple layout containing a VideoView.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black"
android:gravity="center" >

<VideoView
    android:id="@+id/videoPlayer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

</RelativeLayout>

活动使用此布局创建一个片段来启动VideoView

The Activity that uses this layout creates a Fragment to start the VideoView

public class VideoPlayerActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_video_player);
            createNewWorkerFragment();
    }

        private void createNewWorkerFragment() {
            FragmentManager fragmentManager = getFragmentManager();
            VideoPlayerActivityWorkerFragment workerFragment = (VideoPlayerActivityWorkerFragment)fragmentManager.findFragmentByTag(VideoPlayerActivityWorkerFragment.Name);
            if (workerFragment == null) {
                workerFragment = new VideoPlayerActivityWorkerFragment();
                fragmentManager.beginTransaction()
                               .add(workerFragment, VideoPlayerActivityWorkerFragment.Name)
                               .commit();
            }
        }
}

VideoPlayerActivityWorkerFragment 如下:

public class VideoPlayerActivityWorkerFragment extends Fragment {

     public static String Name = "VideoPlayerActivityWorker";
     private VideoView mVideoPlayer;

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setRetainInstance(true);
         mVideoPlayer = (VideoView) mActivity.findViewById(R.id.videoPlayer);
         mVideoPlayer.setVideoPath(mActivity.getIntent().getExtras().getString("path"));
         MediaController controller = new MediaController(mActivity);
         controller.setAnchorView(mVideoPlayer);
         mVideoPlayer.setMediaController(controller);
         mVideoPlayer.requestFocus();
         mVideoPlayer.start();
     }

     @Override
     public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
     }

     @Override
     public void onDetach() {
        super.onDetach();
        mActivity = null;
     }
}

这是我遇到的问题, VideoPlayerActivity 启动时 VideoPlayerActivityWorkerFragment 创建和 VideoView 开始播放,但是当我旋转设备的视频停止,将无法播放,整个查看似乎从布局走了。由于 setRetainInstance(真); 我认为 VideoView 将继续发挥。有人可以让我知道我做错了什么?我在别处使用这种模式(不与 VideoView ),并成功地使旋转的情况发生。

This is the issue I'm having, when the VideoPlayerActivity starts the VideoPlayerActivityWorkerFragment is created and the VideoView starts playing, however when I rotate the device the video stops and will not play, the entire View seems gone from the layout. Due to setRetainInstance(true); I thought that the VideoView would continue to play. Can someone let me know what I'm doing wrong? I have used this pattern elsewhere (not with a VideoView) and it successfully allows rotation to happen.

我不愿意使用的android:configChanges =keyboardHidden |方向|屏幕尺寸或类似的方法,我想处理好与<$ C $的方向变化C>片段秒。

I am unwilling to use the android:configChanges="keyboardHidden|orientation|screenSize" or similar methods, I would like to handle the orientation change with Fragments.

感谢您提前对您有所帮助。

Thank you in advance for your help.

我最终选择的解决我这样做,因为它的工作原理。的videoview和控制器需要每个onCreateView被调用时被重新创建并需要在onResume被设置,并在的onPause条记录的播放位置。然而,再现是在旋转过程中波动。该解决方案不是最佳的,但它的工作。

I ended up picking the solution I did because it works. The videoview and controller need to be recreated each time onCreateView is called and the playback position needs to be set in onResume and recored in onPause. However, the playback is choppy during the rotation. The solution is not optimal but it does work.

推荐答案

请参阅 mVideoPlayer =(VideoView)mActivity.findViewById(R.id.videoPlayer);
你的 VideoView 是由被破坏并重新创建轮换的活动创建。这意味着正在创建一个新的 R.id.videoPlayer VideoView。因此,当地的 mVideoPlayer 刚刚被覆盖在上面的线路。您的片段需要创建 VideoView 在其onCreateView()方法。即使是这样,这可能是不够的。因为视图是天生与他们拥有的上下文联系起来。也许是一个明确的暂停,检测和重视,认为发挥将是一个更好的路要走。

See mVideoPlayer = (VideoView) mActivity.findViewById(R.id.videoPlayer); Your VideoView is created by the activity which is being destroyed and recreated on rotation. This means a new R.id.videoPlayer VideoView is being created. So your local mVideoPlayer is just being overwritten in your above line. Your fragment needs to create the VideoView in its onCreateView() method. Even then, this may not suffice. Because Views are inherently linked with their owning Context. Perhaps an explicit pause, detect and attach, play of the view would be a better way to go.

这篇关于片段保留不保留的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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