title: StartActivityForResult - check for existence of intent extra or use custom result code?
body: <p>I have a search button in MainActivity which launches SearchActivity. In SearchActivity, the user can either choose from one of the predefined categories listed, or can enter in a search query. The SearchActivity will return a different extra depending on which way the user searches. The code that runs in MainActivity will depend on which extra is returned.</p>
  
  <p>I'm debating which way is better or more "correct". I've coded it both ways, and it works either way.</p>
  
  <p>The first way I coded it was to check for the existence of the intent extra:</p>
  
  <pre><code>protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == GET_SEARCH_REQUEST_CODE) {
          if (resultCode == RESULT_OK) {
              if (data.getExtras().containsKey("extra1")) {
                  extra1 = data.getStringExtra("extra1");
              }
              if (data.getExtras().containsKey("extra2")) {
                  extra2 = data.getStringExtra("extra2");
              }
          }
          // plus rest of code for checking for RESULT_CANCELED
      }
  }
  </code></pre>
  
  <p>The other way I coded it is by using custom result codes. The result codes <code>RESULT_OK_CATEGORY</code> and <code>RESULT_OK_SEARCH</code> are public static variables in MainActivity so that they can be accessed from SearchActivity, and sent back as a result code through <code>setResult(MainActivity.RESULT_OK_CATEGORY, intent)</code> or <code>setResult(MainActivity.RESULT_OK_SEARCH, intent)</code> respectively.</p>
  
  <pre><code>protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == GET_SEARCH_REQUEST_CODE) {
          if (resultCode == RESULT_OK_CATEGORY) {
              extra1 = data.getStringExtra("extra1");
          } else if (resultCode == RESULT_OK_SEARCH) {
              extra2 = data.getStringExtra("extra2");
          }
          // plus rest of code for checking for RESULT_CANCELED
      }
  }
  </code></pre>
  
  <p>Which way is better, and why? Checking for the existence of the extra, or checking for a custom result code?</p>