title: How to save the state of the progress of the progress bar when the user clicks on the back button in android
body: <p>I was wondering on what would happen when the user clicks the back button when the progress is going on. It came to my mind that the current progress <strong>should be saved</strong> when the user exits the current activity and goes back to the previous activity or any other application. I have written the code for downloading the file and showing the progress bar when the user taps the sync button. It is as follows:</p>
  
  <pre><code>public class DownloadVideoTask extends AsyncTask&lt;String, String, String&gt;{
  
      @Override
      protected String doInBackground(String... downloadurl) {
          int count;
          try{
              URL url = new URL(downloadurl[0]);
              URLConnection connection = url.openConnection();
              connection.connect();
              int lengthOfFile = connection.getContentLength();
              Log.d(TAG, "Length of file: " + lengthOfFile);
              InputStream inputStream = new BufferedInputStream(url.openStream());
              OutputStream outputStream = new FileOutputStream("/sdcard/drona_video.mp4");
  
              byte data[] = new byte[1024];
              long total = 0;
              while((count = inputStream.read(data))!= -1){
                  total += count;
                  publishProgress("" + (int)((total*100)/lengthOfFile));
                  outputStream.write(data, 0, count);
              }
              dbAdapter.insertVideoBytesInVideoDownloadsTable(id, data);
              //bundle.putByteArray("videoBytesArray", data);
              outputStream.flush();
              outputStream.close();
              inputStream.close();
          }catch(Exception e){
              return null;
          }
          return null;
      }
  
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          //showDialog(DIALOG_DOWNLOAD_PROGRESS);
          syncBtn.setEnabled(false);
          progressBar.setMax(100);
          progressBar.setVisibility(View.VISIBLE);
          txtProgressPercentage.setVisibility(View.VISIBLE);
      }
  
      @Override
      protected void onPostExecute(String result) {
          //dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
          //mProgressDialog.dismiss();
          progressBar.setVisibility(View.INVISIBLE);
          progressBar.setSaveEnabled(true);
          txtProgressPercentage.setVisibility(View.INVISIBLE);
          syncBtn.setVisibility(View.INVISIBLE);
          playBtn.setVisibility(View.VISIBLE);
      }
  
      @Override
      protected void onProgressUpdate(String... progress) {
          Log.d(TAG, progress[0]);
          //mProgressDialog.setProgress(Integer.parseInt(progress[0]));
          progressBar.setProgress(Integer.parseInt(progress[0]));
          txtProgressPercentage.setText(progress[0] + "%");
      }
  
  
  }
  </code></pre>
  
  <p>I need some code to put between the above mentioned code such that it saves the state of the progress bar when the activity exits while the user has pressed the back button and when the activity gets open it resumes from where it has been stopped. Please help me.</p>