title: Start fragment from Service
body: <p>I'm trying to make a Youtube video play on top of all activities so I used SYSTEM_ALERT_WINDOW, Now I am stuck trying to start the YoutubeSupportFragment from the service, here is my onCreate method in the FloatingWindow Service</p>
  
  <pre><code>public class FloatingWindow extends Service{
  
  @Override
  public void onCreate() {
      super.onCreate();
  
      windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
      linearLayout = new LinearLayout(this);
  
      LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
  
      linearLayout.setLayoutParams(linearLayoutParams);
  
      final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(400,400, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
      parameters.x = 0;
      parameters.y = 0;
      parameters.gravity = Gravity.CENTER | Gravity.CENTER;
  
      linearLayout.addView(stop);
      windowManager.addView(linearLayout, parameters);
  
  
      linearLayout.setOnTouchListener(new View.OnTouchListener() {
  
          private WindowManager.LayoutParams updatedParameters = parameters;
          int x, y;
          float touchedX, touchedY;
  
          @Override
          public boolean onTouch(View v, MotionEvent event) {
  
              switch (event.getAction()) {
                  case MotionEvent.ACTION_DOWN:
                      x = updatedParameters.x;
                      y = updatedParameters.y;
  
                      touchedX = event.getRawX();
                      touchedY = event.getRawY();
                      break;
  
                  case MotionEvent.ACTION_MOVE:
                      updatedParameters.x = (int) (x + (event.getRawX() - touchedX));
                      updatedParameters.y = (int) (y + (event.getRawY() - touchedY));
  
                      windowManager.updateViewLayout(linearLayout, updatedParameters);
                      break;
  
                  default:
                      break;
              }
  
              return false;
          }
      });
  
  
  }
  </code></pre>
  
  <p>And here is my XML file that I want displayed on as a floating window</p>
  
  <pre><code>&lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/linearLayout"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"
      android:background="@color/COLOR_BACKGROUND"&gt;
  
  &lt;fragment
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/youtube_fragment"
      android:name="com.example.youtube.YoutubePlayerView"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The problem is, on onCreate i tried inflating the linearLayout with the XML file, i'm getting a ClassNotFoundException for the fragment, i think its obvious since I didn't instantiate the fragment. </p>
  
  <p>I also can't use FragmentTransaction to add the XML file to my linearLayout because i can't access FragmentManager from within the service class. </p>
  
  <p>Any ideas how i can get the youtube player to play as a floating window??</p>
  
  <p>This is what I'm trying to achieve
  <a href="https://i.stack.imgur.com/Bx2Ie.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Bx2Ie.png" alt="Youtube floating window"></a></p>