title: how to stop or restart a new instance of YouTubePlayerFragment, android, Google Youtube API for Android
body: <p>in the code below I want to start a YouTubePlayerFragment object from the google's youtube API for Android. however it will only start when i first run the app. i have no control of the lifecycle of the fragment called YouTubePlayerFragment of Google's API, it extends Fragment, and is basically a fragment with a youtube player inside of it.</p>
  
  <p>i have no ability to control this youtube player.  the only way to get the youtubeplayer object and get control of the lifecycle of the player is to call the   onInitializationSuccess() method, which you can't do because that method is called by the operating system.  it is a callback method.</p>
  
  <p>I can't figure out why I am getting all these problems with null pointer exceptions and other errors.  basically I remove the video from visibility and later show the fragment visible only after the user clicks on a link in WebView where the substring "youtube" is contained in the URL.  and then it would set the youtube fragment visible.  i wanted to get the URL and start playing the video on that link.</p>
  
  <p>if i try Fragment transaction remove() and add() methods I get null pointer and crashes.</p>
  
  <p>and if i don't remove the fragment. then the next time the user clicks on a youtube link in webview it starts playing the same youtube video as before in an earlier link not the link just clicked.  and it is starts the playback in the same position where it was left off before.</p>
  
  <p>all I want to do is KILL this youtube API fragment and restart a new one.</p>
  
  <p>how do I fix this problem?</p>
  
  <pre><code>  public class MainActivity extends YouTubeFailureRecoveryActivity {
  
     @Override
     public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);
  
     handler = new Handler();
     storedVideoUrl = ""; 
     playTube = false;
  
     setContentView(R.layout.fragments_demo);
     pageView = (WebView) findViewById(R.id.webView1);
     coverImage = (ImageView) findViewById(R.id.imageView1);
  
     findViewById(R.id.youtube_fragment).setVisibility(View.INVISIBLE);
  
     youTubePlayerFragment = (YouTubePlayerFragment)
     getFragmentManager().findFragmentById(R.id.youtube_fragment);
  
  youTubePlayerFragment.initialize(DeveloperKey.DEVELOPER_KEY, MainActivity.this);
  
  coverImage.setVisibility(View.INVISIBLE);
  
  pageView.loadUrl("http://www.example.com/");  
  
  WebSettings webSettings = pageView.getSettings();
  
  //ebSettings.setJavaScriptEnabled(true);
  
  
  WebViewClient mWebClient = new WebViewClient()
  {
      // Override page so it's load on my view only
      @Override
      public boolean shouldOverrideUrlLoading(WebView  view, String  url)
      {
          storedVideoUrl = url;
          playTube = true;
  
       if ( url.contains("youtube") == true ){
          // Load new URL Don't override URL Link
  
           videoRoot = youTubePlayerFragment.getView();
           videoUrl = url.substring(29, 40);
           coverImage.setVisibility(View.VISIBLE);
           videoRoot = youTubePlayerFragment.getView();
  
           if(videoRoot==null){
           videoRoot.setVisibility(View.INVISIBLE);
           }else{
               videoRoot.setVisibility(View.VISIBLE);
           }
  
              return true;
  
      } else{
  
       coverImage.setVisibility(View.INVISIBLE);
  
  
       // Return true to override url loading (In this case do nothing).
       return false;
       // if return is true, do nothing. don't load url
  
      }
   }
  
  
    };
  
     pageView.setWebViewClient(mWebClient);
  
   }  // end onCreate 
  
  
    public YouTubePlayer.Provider getPlayer() {
  return player;
   }
  
  
   @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
    boolean wasRestored) {
  if (!wasRestored) {
  
   player.loadVideo("videoUrl");
      }
     }
  
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_BACK)
  {
  
      if (pageView.canGoBack()){
  
          if((videoRoot.getVisibility())==View.VISIBLE){
               coverImage.setVisibility(View.INVISIBLE);
               videoRoot.setVisibility(View.INVISIBLE);
  
               return true;
          }else {
  
          pageView.goBack();
  
          return true;
          }
      }
  }
  
  return super.onKeyDown(keyCode, event);
   }
  
    @Override
    protected Provider getYouTubePlayerProvider() {
  // TODO Auto-generated method stub
  return null;
    }
  
    }  // end fragment demo activity
  </code></pre>