title: YouTube API Playlists in Fragments?
body: <p>So I've been trying to put a YouTube playlist into my app for days now, and I can't seem to figure it out. I have been following <a href="http://www.truiton.com/2015/03/android-youtube-api-example-channels/" rel="nofollow">this tutorial</a>, but the problem is my app is in fragments because of the tab view, like this:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>public class FragmentTab2 extends Fragment {
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          // Get the view from fragmenttab1.xml
          View view = inflater.inflate(R.layout.fragmenttab2, container, false);
      }
  
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>How would I do to have the code below, provided by the tutorial, to work on my fragment?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>public class TruitonYouTubeAPIActivity extends YouTubeBaseActivity implements
          YouTubePlayer.OnInitializedListener {
  
      private YouTubePlayer YPlayer;
      private static final String YoutubeDeveloperKey = "AIzaSyDhEeq_FYAdCzMMSigmMcRwF_nQEhUXS-0";
      private static final int RECOVERY_DIALOG_REQUEST = 1;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_you_tube_api);
  
          YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
          youTubeView.initialize(YoutubeDeveloperKey, this);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.you_tube_api, menu);
          return true;
      }
  
      @Override
      public void onInitializationFailure(YouTubePlayer.Provider provider,
                                          YouTubeInitializationResult errorReason) {
          if (errorReason.isUserRecoverableError()) {
              errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
          } else {
              String errorMessage = String.format(
                      "There was an error initializing the YouTubePlayer",
                      errorReason.toString());
              Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
          }
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (requestCode == RECOVERY_DIALOG_REQUEST) {
              // Retry initialization if user performed a recovery action
              getYouTubePlayerProvider().initialize(YoutubeDeveloperKey, this);
          }
      }
  
      protected YouTubePlayer.Provider getYouTubePlayerProvider() {
          return (YouTubePlayerView) findViewById(R.id.youtube_view);
      }
  
      @Override
      public void onInitializationSuccess(Provider provider,
                                          YouTubePlayer player, boolean wasRestored) {
          YPlayer = player;
   /*
   * Now that this variable YPlayer is global you can access it
   * throughout the activity, and perform all the player actions like
   * play, pause and seeking to a position by code.
   */
          if (!wasRestored) {
              YPlayer.cueVideo("2zNSgSzhBfM");
          }
      }
  
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Thanks in advance.</p>