title: startActivityForResult can't work well when setResult is placed in onPause(), onStop() or onDestroy()
body: <p>When I put <em>setResult()</em> in the <em>onClickListener</em> of a button, that works. However, if I put it in <em>onPause()</em>, <em>onStop()</em> or <em>onDestroy()</em>, that never works.  </p>
  
  <p>I got really confused about that.  </p>
  
  <p>Here is the code of MainActivity.   </p>
  
  <pre><code>package com.example.hellotest;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.Toast;
  
  public class MainActivity extends Activity {
      private static final String TAG = MainActivity.class.getSimpleName();
  
      private Button button;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          button = (Button) findViewById(R.id.button);
          button.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View view) {
                  startActivityForResult(new Intent(getApplicationContext(), SecondActivity.class), 0);
              }
          });
      }
  
      @Override
       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if(resultCode == RESULT_OK) {
              Toast.makeText(getApplicationContext(), "hello world", Toast.LENGTH_SHORT).show();
              Log.d(TAG, "RESULT_OK");
          } else {
              Toast.makeText(getApplicationContext(), "result not ok", Toast.LENGTH_SHORT).show();
              Log.d(TAG, "RESULT_CANCELED");
          }
      }
  }
  </code></pre>
  
  <p>Here is the SecondActivity.</p>
  
  <pre><code>package com.example.hellotest;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  
  public class SecondActivity extends Activity {
      private static final String TAG = SecondActivity.class.getSimpleName();
  
      private Button button;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_second);
  
          button = (Button) findViewById(R.id.button);
          button.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
  //              setResult(RESULT_OK);
                  Log.d(TAG, "button on click");
              }
          });
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.second, menu);
          return true;
      }
  
      @Override
      protected void onDestroy() {
          setResult(RESULT_OK);
          Log.d(TAG, "onDestroy");
          super.onDestroy();
      }
  
      @Override
      protected void onPause() {
  //      setResult(RESULT_OK);
          Log.d(TAG, "onPause");
          super.onPause();
      }
  
      @Override
      protected void onStop() {
  //      setResult(RESULT_OK);
          Log.d(TAG, "onStop");
          super.onStop();
      }
  }
  </code></pre>
  
  <p>And here is the Log of which I put <em>setResult()</em> in the <em>onClick()</em></p>
  
  <pre><code>11-21 00:07:54.243: D/SecondActivity(4790): button on click
  11-21 00:07:56.364: D/SecondActivity(4790): onPause
  11-21 00:07:57.813: D/SecondActivity(4790): onStop
  11-21 00:07:57.813: D/SecondActivity(4790): onDestroy
  
  11-21 00:07:56.404: D/MainActivity(4790): RESULT_OK
  </code></pre>
  
  <p>But if in <em>onDestroy()</em></p>
  
  <pre><code>11-21 00:10:08.125: D/SecondActivity(4846): onPause
  11-21 00:10:09.456: D/SecondActivity(4846): onStop
  11-21 00:10:09.456: D/SecondActivity(4846): onDestroy
  
  11-21 00:10:08.176: D/MainActivity(4846): RESULT_CANCELED
  </code></pre>
  
  <p>I have no idea about that.</p>