title: finishActivity() - closing all activities with exact request code
body: <p>I'm new with android but fighting ;) I decided to write the mp3 player. And I have problem with on of my tasks. I would like to choose a file to play from my file browser. It is implemented like this:</p>
  
  <p>Activity of A class starts Activity of B class. Then a few times Activities of B class are started. Everytime it is implemented with startActivityForResult (intent, REQUEST_CODE) - request code is the same each time. From the last activity I would like to come back to the first activity with a result. So I need to close all activities opened so far. Because all of them has the same REQUEST_CODE I did it with finishActivity(REQUEST_CODE) (to close all before the last one) and then finish() (to close the newest one). The result is the same as with finish() only - only the last activity closes. Can you please tell me where my mistake is? I would be very greatful. Here is some code related to the problem. First the intent in MainActivity.class is send (in browseFiles method). Then the code of FileListActivity.class is perform. The folowing FileListActivity.class code fragment is at the end. And, at last, onActivityResult in MainActivity.class should be perform.  </p>
  
  <p>MainActivity.class</p>
  
  <pre><code>public void browseFiles (View v) throws IOException {
      Intent intent = new Intent(this, FileListActivity.class);
      intent.putExtra(path, "/");
      startActivityForResult(intent, R.integer.REQUEST_CHOOSE_A_SONG);
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data){
      super.onActivityResult(requestCode,resultCode,data);
      switch(requestCode){
          case R.integer.REQUEST_CHOOSE_A_SONG:
              if(resultCode == RESULT_OK) try {
                  mediaPlayer.setDataSource(this, Uri.parse(data.getStringExtra(path)) );
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
      }
  }
  </code></pre>
  
  <p>FileListActivity.class</p>
  
  <pre><code>if(new File(fileName).isDirectory()){
          intent = new Intent(this, FileListActivity.class);
          intent.putExtra("path", fileName);
          startActivityForResult(intent, R.integer.REQUEST_CHOOSE_A_SONG);
      }
      else {
          intent = new Intent();
          intent.putExtra("path", fileName);
          setResult(RESULT_OK, intent);
          finishActivity(R.integer.REQUEST_CHOOSE_A_SONG);
          Toast.makeText(this, fileName + " set to play!", Toast.LENGTH_SHORT).show();
          finish();
      }
  </code></pre>
  
  <p>Thank you in advance!
  Malvinka</p>