Activity being created twice on android
<p>First of all, I'm new to Android Development so please have patience with me.</p>

<p>I'll start from the UI, I have a button that once you tap it, starts an activity for a result.</p>

<pre><code>public class GUIActivity extends Activity

    @Override
    public void onClick(....){
        Intent intent = new Intent(getApplicationContext(), GetImageActivity.class);
        intent.putExtra("action", FROM_CAMERA);
        startActivityForResult(intent, GET_IMAGE);
    }

    @Override
    onActivityResult(int requestCode, int resultCode, Intent data){
        Log(TAG, "onActivityResult");
        //handle result
    }

}
</code></pre>

<p>The <code>GetImageActivity</code> class is a wrapper for two other activities, one to capture an image from the camera and other to get it from the gallery. It returns and Uri object of the selected image.</p>

<pre><code>public class GetImageActivity extends Activity{
    private Uri mediaUri;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        int action =  extras.getInt("action");

        Log.d(TAG, "onCreate");

        switch(action){
            case FROM_CAMERA:
                mediaUri = Uri.fromFile(new File(....));
                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, mediaUri);

                Log.d(TAG, "Calling camera activity"
                startActivityForResult(intent, action);
                break;
            case FROM GALLERY:
                //...
        }            
    }

    @Override
    onActivityResult(int requestCode, int resultCode, Intent data){
        Log.d(TAG, "onActivityResult");

        switch(requestCode){
            case FROM_CAMERA:
                if(resultCode == Activity.RESULT_OK){
                    Intent data = new Intent();
                    data.putExtra("uri", mediaUri);
                    setResult(Activity.RESULT_OK, data);
                    finish();
                }else{
                    Log.e(TAG, "Camera activity failed!");
                    setResult(Activity.RESULT_CANCELED);
                    finish();
                }
                break;

            case FROM_GALLERY:
                //...

        }
    }
}
</code></pre>

<p>This is what is expected to happen when the user clicks on the button:</p>

<ul>
<li>The camera activity should start.</li>
<li>Once the user takes a picture it should send back its URI to the GUI class.</li>
</ul>

<p>Sometimes (it's usually a 50% chance) it works at expected, but other times this is what happens:</p>

<ul>
<li>The camera activity starts.</li>
<li>The user takes a picture.</li>
<li>The camera activity starts AGAIN.</li>
<li>The user can either take another picture or go back.</li>
<li>Either case, the URI that gets back to the GUI class does not exist.</li>
</ul>

<p>I've added a couple of debug log lines to follow the sequence of events. When I get the bad behaviour this is the output I get:</p>

<ul>
<li>GetImageActivity - onCreate</li>
<li>GetImageActivity - Calling Camera Activity</li>
</ul>

<p>The camera opens, and once I've taken a picture it says:</p>

<ul>
<li>GetImageActivity - onCreate (again)</li>
<li>GetImageActivity - Calling Camera Activity</li>
<li>GetImageActivity - onActivityResult</li>
</ul>

<p>The camera opens for the second time. The user takes another picture and:</p>

<ul>
<li>GetImageActivity - onActivityResult</li>
<li>GUIActivity - onActivityResult</li>
</ul>

<p>So my question is... what could cause the <code>GetImageActivity</code> to be called twice?</p>
