title: OnActivityResult called before OnClick
body: <p>I am trying to create an intent to open the Gallery (which works fine).</p>
  
  <p>However, I need to change the image button to show the image of the picture selected by the user. </p>
  
  <p>The problem is that onActivityResult() method is being called before the galleryIntent() method which is resulting in the result code not being RESULT_OK therefore when the picture is selected, the user is redirected to the application and nothing happens. </p>
  
  <pre><code>public class DataEntryActivity extends AppCompatActivity {
  
  ImageButton imgButton;
  Bitmap bmp;
  private static final int PICTURE_SELECTED = 1;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(com.example.bernine.practicalsessions.R.layout.activity_portal);
      imgButton = (ImageButton) findViewById(com.example.bernine.practicalsessions.R.id.imageButton1);
  }
  
  //Method to start the intent upon clicking the imageButton
  public void galleryIntent(View view) {
      Intent intent = new Intent();
      intent.setAction(android.content.Intent.ACTION_GET_CONTENT);
      intent.setType("image/*");
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivityForResult(intent, PICTURE_SELECTED);
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      Toast.makeText(getApplicationContext(), "Image Opened Before", Toast.LENGTH_LONG).show();
      if (resultCode == RESULT_OK) {
          try {
              InputStream stream = getContentResolver().openInputStream(data.getData());
              bmp = BitmapFactory.decodeStream(stream);
              stream.close();
              Toast.makeText(getApplicationContext(), "Image Opened", Toast.LENGTH_LONG).show();
              imgButton.setImageBitmap(bmp);
          }catch(Exception e)
          {
              Toast.makeText(getApplicationContext(), "Could not open file.", Toast.LENGTH_LONG).show();
  
          }
      }else
      {
          Toast.makeText(getApplicationContext(), "Result not ok", Toast.LENGTH_LONG).show();
      }
      super.onActivityResult(requestCode, resultCode, data);
  }
  }
  </code></pre>
  
  <p>I tried doing android:launchMode="standard" in the activity in the manifest but did not work.</p>