How to retain instance of fragment of fragment playing video after change in orientation?
android, android-youtube-api, fragment, orientation
Solution
The quick and easy solution is to add the following to your video activity, in your manifest:
android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
I have an app that uses this for a video and it works just fine. Everything lays out properly on orientation change, and the activity and fragment instance do not get torn down allowing it to seamlessly continue to play.
If you want the activity to be torn down/recreated, while retaining the fragment instance, please re-read the documentation for `setRetainInstance()`. There are some subtle nuances that you need to be aware of to get this to work properly: http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)
Problem
I have a YouTube API fragment that is statically added in my xml manifest file, i.e. a fragment that has a youtube player inside of it. I do not have a file that extends fragment in my project. In my activity class I put this line of code in the `onCreate` of my activity class: ``` youTubePlayerFragment.setRetainInstance(true); ``` It does not have any affect: when I rotate the screen I get a blank black screen of the fragment. How can I get it to continue playing the fragment after the orientation change? ``` <fragment android:id="@+id/youtube_fragment" android:name="com.google.android.youtube.player.YouTubePlayerFragment" android:layout_width="900dp" android:layout_height="500dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> ```