YouTubePlayerSupportFragment starts duplicate Acti

2019-04-25 04:49发布

问题:

I am using a YouTubePlayerSupportFragment to embed a YouTube video in my app. When embedded it works very well, the video plays, everything is great.

When I tap the Fullscreen button my embedded YouTube video fragment, the first strange thing happens: It instantiates a new copy of the existing enclosing Activity... and I have no idea why.

I have one Activity, RootActivity, which displays several fragments in a ViewPager. The fragment I'm dealing with at the moment is called EasyModePurchaseFragment. That EMPF has the YouTubePlayerSupportFragment embedded in its layout xml:

<fragment
    android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
    android:id="@+id/youtube_fragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
/>

When I tap the Fullscreen button on the YouTubePlayerSupportFragment, an entirely new RootActivity gets instantiated. Why is that?

It's causing me some problems because it doesn't get fully created in the proper way, which means it causes a crash.. and it's no good.

EDIT:

I've also tried intercepting the onFullScreen() event and instead of resizing the view, I just attempt to launch a standalone YouTube player with their provided intents:

Intent intent = YouTubeStandalonePlayer.createVideoIntent(getActivity(), myDeveloperKey, videoId);
getActivity().startActivity(intent);

Strangely, THAT destroys and recreates my RootActivity as well. My activity doesn't get destroyed and recreated if I launch an intent to open a web URL, why is it getting destroyed and recreated when I launch the YouTubeStandalonePlayer intent?

回答1:

Add to activity that contains YouTubePlayerSupportFragment this attribute

android:configChanges="screenSize|orientation|keyboardHidden"


回答2:

Maybe this will help, I tried pretty hard to find a solution to your specific problem without any luck. It's hard without you posting more of your code to see exactly the YouTube player is set up in your application.

https://github.com/youtube/yt-android-player/blob/master/src/com/examples/youtubeapidemo/FullscreenDemoActivity.java