I want to use YouTubePlayerFragment in my activity with backstack. So when I press back button fragment disappears and on some action in future (button click for example) appear
I've extend YouTubePlayerSupportFragment to store player instance and correctly release it in onDestroyView
public class YouTubePlayerFragment2 extends YouTubePlayerSupportFragment {
private YouTubePlayer mPlayer;
public static YouTubePlayerFragment2 newInstance(){
return new YouTubePlayerFragment2();
}
@Override
public void initialize(String s, YouTubePlayer.OnInitializedListener onInitializedListener) {
YouTubePlayer.OnInitializedListener listener = new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
onInitializedListener.onInitializationSuccess(provider, youTubePlayer, b);
mPlayer = youTubePlayer;
mPlayer.setShowFullscreenButton(false);
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
onInitializedListener.onInitializationFailure(provider, youTubeInitializationResult);
}
};
super.initialize(s, listener);
}
@Override
public void onDestroyView() {
if(mPlayer!=null) {
mPlayer.release();
}
super.onDestroyView();
}
}