title: How to cancle AsyncTask in Android
body: <p>In my application i have 2 <code>AsyncTask</code> methods and i want when <code>activity</code> go to <code>onPause</code> cancel this <code>AsyncTask</code>s and in <code>onResume</code> <strong>start from the beginning</strong>.<br>
  I write below codes, i pressed home button and application go to <code>onPause</code> and open again this app from recent apps <br>
  My mean is call <code>onPause</code> and <code>onResume</code> !<br></p>
  
  <p>But when application run again (<strong>from recent app</strong>), call <code>onResume</code> but not start <code>AsyncTask</code> from the beginning!<br></p>
  
  <p><strong>My codes :</strong> </p>
  
  <pre><code>@Override
  protected void onResume() {
      super.onResume();
      new Handler(getMainLooper()).postDelayed(() -&gt; {
          //Get compress or upload
          if (bundle == null) {
              //Get video from file
              getLoadOriginalVideos();
              testIsSendPlans = testId;
          }
      }, 500);
  }
  
  @Override
  protected void onPause() {
      super.onPause();
      new GetOriginalVideosAsync().cancel(true);
      new GetCompressedVideosAsync().cancel(true);
  }
  
  private void getLoadOriginalVideos() {
      if (videoList.isEmpty()) {
          File directory = new File(Environment.getExternalStorageDirectory() + File.separator + APPDIR);
          ArrayList&lt;File&gt; filesList = new ArrayList&lt;File&gt;();
          if (directory.isDirectory() &amp;&amp; directory.exists()) {
              filesList.addAll(Arrays.asList(getOriginalVideos(directory.listFiles())));
          }
          new GetOriginalVideosAsync().execute(filesList.toArray(new File[filesList.size()]));
      }
  }
  
  @SuppressLint("StaticFieldLeak")
  class GetOriginalVideosAsync extends AsyncTask&lt;File[], Integer, ArrayList&lt;Video&gt;&gt; {
      File[] files;
      ContentResolver resolver;
  
      GetOriginalVideosAsync() {
          resolver = context.getContentResolver();
      }
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
      }
  
      @Override
      protected void onPostExecute(ArrayList&lt;Video&gt; videos) {
          Collections.sort(videos, Collections.reverseOrder());
          getLastOriginalVideo(addVideos(videos));
      }
  
      private ArrayList&lt;Video&gt; addVideos(ArrayList&lt;Video&gt; videos) {
          return new ArrayList&lt;&gt;(videos);
      }
  
      @Override
      protected void onProgressUpdate(Integer... values) {
          super.onProgressUpdate(values);
  
      }
  
      @Override
      protected ArrayList&lt;Video&gt; doInBackground(File[]... arg) {
          files = arg[0];
          for (File file : files) {
              if (!file.isDirectory() &amp;&amp; isVideoFile(file.getPath())) {
                  videoList.add(new Video(file.getName(), file, new Date(file.lastModified())));
              }
          }
          return videoList;
      }
  }
  
  @SuppressLint("StaticFieldLeak")
  class GetCompressedVideosAsync extends AsyncTask&lt;File[], Integer, ArrayList&lt;Video&gt;&gt; {
      File[] files;
      ContentResolver resolver;
  
      GetCompressedVideosAsync() {
          resolver = context.getContentResolver();
      }
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
      }
  
      @Override
      protected void onPostExecute(ArrayList&lt;Video&gt; videos) {
          Collections.sort(videos, Collections.reverseOrder());
          getLastCompressedVideo(addVideos(videos));
      }
  
      private ArrayList&lt;Video&gt; addVideos(ArrayList&lt;Video&gt; videos) {
          return new ArrayList&lt;&gt;(videos);
      }
  
      @Override
      protected void onProgressUpdate(Integer... values) {
          super.onProgressUpdate(values);
  
      }
  
      @Override
      protected ArrayList&lt;Video&gt; doInBackground(File[]... arg) {
          files = arg[0];
          for (File file : files) {
              if (!file.isDirectory() &amp;&amp; isVideoFile(file.getPath())) {
                  videoListCompressed.add(new Video(file.getName(), file, new Date(file.lastModified())));
              }
          }
          return videoListCompressed;
      }
  }
  </code></pre>
  
  <p>How can i fix it and in <code>onResume</code> <strong>start from beginning</strong> this <code>AsyncTask</code>s ?</p>