DialogFragment中的YouTubePlayerFragment生命周期 [英] YouTubePlayerFragment lifecycle in a DialogFragment

查看:127
本文介绍了DialogFragment中的YouTubePlayerFragment生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试嵌入 YouTubePlayerFragment 放入 DialogFragment .我可以一次启动对话框并在其中显示YouTubePlayer,但是第二次对话框总是崩溃(无论我做什么).我认为这是一个生命周期问题,我根本不了解. 我正在使用AndroidAnnotations,问题是DialogFragment的视图始终在由AndroidAnnotations生成的onCreateView方法中创建.

I am trying to embed a YouTubePlayerFragment into a DialogFragment. I am able to start the dialog one time and show the YouTubePlayer in it, but the second time it always crashes (no matter what I do). I think it is a lifecycle problem, which I simply don't understand. I am using AndroidAnnotations and the problem is that the view of the DialogFragment is always created in the onCreateView method, which is generated by AndroidAnnotations.

在这种情况下,有人知道如何处理DialogFragment的生命周期吗?

Does anyone know how to handle the lifecycle of a DialogFragment in this case?

这是从AndroidAnnotations生成的代码:

This is the generated code from AndroidAnnotations:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    contentView_ = super.onCreateView(inflater, container, savedInstanceState);
    if (contentView_ == null) {
        contentView_ = inflater.inflate(layout.video_fragment, container, false);
    }
    return contentView_;
}

这是我到目前为止所拥有的:

This is what I have so far:

public class VideoFragmentDialog extends DialogFragment implements YouTubePlayer.OnInitializedListener {

    private static final String DEVELOPER_KEY = "secret";
    private String videoUrl;

    @FragmentById(R.id.youTubePlayerFragment)
    YouTubePlayerFragment youTubePlayerFragment;

    @AfterViews
    void initializeYouTubePlayer() {
        youTubePlayerFragment.setRetainInstance(true);
        youTubePlayerFragment.initialize(DEVELOPER_KEY, this);
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
        if (!wasRestored) {
            youTubePlayer.cueVideo(videoUrl);
        }
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public String getVideoUrl() {
        return videoUrl;
    }

    public void setVideoUrl(String videoUrl) {
        this.videoUrl = videoUrl;
    }
}

这是堆栈跟踪:

Caused by: java.lang.IllegalArgumentException: Binary XML file line #10: Duplicate id 0x7f0a0281, tag null, or parent id 0x7f0a0280 with another fragment for com.google.android.youtube.player.YouTubePlayerFragment
    at android.app.Activity.onCreateView(Activity.java:4248)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)

推荐答案

我想这是因为您正在片段中使用片段(嵌套片段)而不使用getChildFragment() 在此处查看如何执行此操作的示例:嵌套片段

I guess it's because you are using a fragment inside a fragment (Nested fragments) without using getChildFragment() Look here for an example how to do it : nested fragments

这篇关于DialogFragment中的YouTubePlayerFragment生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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