title: Handling OnPause and OnResume inside fragments android
body: <p>I am trying to communicate with USB using InputOutput stream, I use constructor and make the communication part in a common class and I do use it inside a fragment, if i use it inside an activity it works properly if i use inside a fragment, the application hangs to overcome the problem i terminated the connection while <strong>OnPause</strong> but while <strong>resume</strong> connection not retains only on fragment same logic works on activity, I use some animations inside like Video is that be a reason?</p>
  
  <pre><code>@SuppressLint("ValidFragment")
  public class Run_Screen extends Fragment implements OnClickListener {
      Usb_Communciation usbCom;
      RelativeLayout R_RelativeLayout, Run_Screen_Layout, Screen_Lock_Layout;
      static SharedPreferences sharedpreferences;    
      Animation wipe;
      static int machine;
      MyLog myLog = new MyLog();
      View root = null;
      static Activity thisActivity = null;
  
      @Override
      public void onAttach(Activity activity) {
          act = activity;
          super.onAttach(activity);
          thisActivity = Run_Screen.this.getActivity();
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          if (Vars.Screen_Size == 400) {
              root = inflater.inflate(R.layout.run_screen_400, container, false);
          } else if (Vars.Screen_Size == 600) {
              root = inflater.inflate(R.layout.run_screen_600, container, false);
          } else if (Vars.Screen_Size == 800) {
              root = inflater.inflate(R.layout.run_screen_800, container, false);
          } else {
              root = inflater.inflate(R.layout.run_screen, container, false);
          }
  
          sharedpreferences = this.getActivity().getSharedPreferences(Vars.MyPre, Context.MODE_PRIVATE);
          GlobalClass.Current_Screen = "Run_Screen";
          cmg = new Comment_Generate(act);
          R_Wiper = (ImageButton) root.findViewById(R.id.R_Wiper);
          wipe = AnimationUtils.loadAnimation(act, R.anim.wipe);
          wipe.setAnimationListener(new AnimationListener() {
              @Override
              public void onAnimationEnd(Animation arg0) {
                  // TODO Auto-generated method stub
                  Wiper_anim.setVisibility(View.INVISIBLE);
                  R_Wiper.setVisibility(View.VISIBLE);
              }
              @Override
              public void onAnimationRepeat(Animation arg0) {
                  // TODO Auto-generated method stub
              }
              @Override
              public void onAnimationStart(Animation arg0) {
                  // TODO Auto-generated method stub
                  Wiper_anim.setVisibility(View.VISIBLE);
              }
          });
  
          R_Pause_Play.setOnClickListener(this);
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      }
  
      @Override
      public void onStart() {
          super.onStart();
      }
  
      @Override
      public void onResume() {
          super.onResume();
          usbCom = new Usb_Communciation(Run_Screen.this.getActivity(), getActivity().getIntent());      
      }
  
      @Override
      public void onPause() {
          super.onPause();
          Usb_Communciation.AppDisconnect(Run_Screen.this.getActivity());
      }
  
  
      public void CheckFunc(){
          if(Vars.ChangeMAchine==1){
              R_Progress.setVisibility(View.VISIBLE);
              R_Separator.setVisibility(View.VISIBLE);
              R_Run.setVisibility(View.INVISIBLE);
              R_Pause.setVisibility(View.VISIBLE);
              R_Stop.setVisibility(View.VISIBLE);
              R_Pause_Play.setVisibility(View.INVISIBLE);
          }
  
      }
  
      @Override
      public void onClick(View v) {
          // TODO Auto-generated method stub
          switch (v.getId()) {
              case R.id.R_Pause_Play:
                  DisableAll();
                  Reset_Machine_State(-2);
                  Vars.ChangeMAchine=1;
                  CheckFunc();
                  R_Progress.setVisibility(View.VISIBLE);
                  Send_Comment(Vars.C_Start_Machine, 0, 0);
                  myLog.Cmd("Write", Vars.C_Start_Machine, "0", "0",
                          "Start The Machine");
                  break;
          }
      }
  
      private void Send_Comment(String Comment_No, int Address, int Data) {
          Sending_Comment = cmg.Generate_Comment_for(Comment_No, Address, Data);
          usbCom.Send_message(Sending_Comment);
      }
  
  
      public void SetUserLevelControls() {
          Vars.CurrentUserLevel = sharedpreferences.getInt(Vars.UserLevel, 5);
          if (Permission.Launcher_Operator_Screen_Defect_Height &lt;= Vars.CurrentUserLevel) {
              Screen_Lock_Layout.setVisibility(View.GONE);
          } else {
              Screen_Lock_Layout.setVisibility(View.VISIBLE);
          }
  
          if (Permission.Launcher_Operator_Screen_General &lt;= Vars.CurrentUserLevel) {
              R_Run.setOnClickListener(this);
              R_Pause.setOnClickListener(this);
              R_Stop.setOnClickListener(this);
              R_Pause_Play.setOnClickListener(this);
              Run_Lock.setVisibility(View.GONE);
          } else {
              R_Run.setOnClickListener(null);
              R_Pause.setOnClickListener(null);
              R_Stop.setOnClickListener(null);
              R_Pause_Play.setOnClickListener(null);
              Run_Lock.setVisibility(View.VISIBLE);
          }
  
      }
  
  }
  </code></pre>
  
  <p>constructor class </p>
  
  <pre><code>public Usb_Communciation(Context mContext, Intent intent) {
          try {
              PackageManager manager = mContext.getPackageManager();
              PackageInfo info = manager.getPackageInfo(mContext.getPackageName(), 0);
              Log.d(TAG, "Info:" + info.packageName + "\n" + info.versionCode + "\n" + info.versionName);
          } catch (PackageManager.NameNotFoundException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }
          accessoryManager = new USBAccessoryManager(handler, USBAccessoryWhat);
          accessoryManager.enable(mContext, intent);
      }
  </code></pre>
  
  <p>AppDisconnect</p>
  
  <pre><code> public static void AppDisconnect(Context context) {
          switch(firmwareProtocol) {
              case 2:
                  byte[] commandPacket = new byte[2];
                  commandPacket[0] = (byte) APP_DISCONNECT;
                  commandPacket[1] = 0;
                  accessoryManager.write(commandPacket);
                  break;
          }
  
          try {
              while(accessoryManager.isClosed() == false) {
                  Thread.sleep(2000);
              }
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
  
  
          accessoryManager.disable(context);
          disconnectAccessory();
      }
  </code></pre>