How to return result 'in a natural way' when the called the called Activity exits?
<p><strong>What Is Working Fine</strong></p>

<p>I have 2 activities in my app. First activity calls second actifity for results.</p>

<p>Second activity shows a new layout and lets user perform certain actions. There is a "OK" button. When user presses this button, second activity is finished and user goes back to first activity.</p>

<p>Under the hood, first activity calls second ativity like this:</p>

<pre><code>Intent intent = new Intent(this, NextAct.class);
intent.putExtra("input", input);
this.startActivityForResult(intent, 99);
</code></pre>

<p>On "OK" button press, second activity returns with result like this:</p>

<pre><code>Intent intent = new Intent();
intent.putExtra("output", output);
setResult(RESULT_OK, intent);
finish();
</code></pre>

<p>After that, first activity's <em>onActivityResult</em> is called with results successfully:</p>

<pre><code>protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // requestCode is 99
    // resultCode is -1
    // data holds my output
}
</code></pre>

<p>Above setup is working perfectly</p>

<p><strong>What Is Not Working</strong></p>

<p>Now I had a simple requirement, I wanted the user to close the second activity not with an "OK" button, but in a natural way with "HARDWARE BACK" button.</p>

<p>I tried moving the <em>setResult logic</em> in <em>onStop</em> and <em>onDestroy</em> methods of second activity, but it turned out that <em>onActivityResult</em> of first activity is called before <em>onStop</em> or <em>onDestroy</em> methods of second activity and as a result <em>setResult logic</em> does not get a chance to run at all.</p>

<p>Then I tried moving the <em>setResult logic</em> in <em>onPause</em> method of second activity like this</p>

<pre><code>protected void onPause() {
  super.onPause();
  Intent intent = new Intent();
  intent.putExtra("output", output);
  setResult(RESULT_OK, intent);
  //finish();  enabling or disabling this does not work
}
</code></pre>

<p>But although <em>onPause</em> is being called well before <em>onActivityResult</em> and <em>setResult logic</em> runs properly, still I get all null values in <em>onActivityResult</em></p>

<pre><code>protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // requestCode is 99
    // resultCode is 0
    // data comes as null
}
</code></pre>

<p><strong>I need to know why this is happening and if <em>onResume</em> is not a proper place to put <em>setResult logic</em>, what is the most natural way to do it?</strong></p>

<p>Thanks much.</p>
