title: Android app losing data during orientation change
body: <p>I've got an app that I've copied from a tutorial that captures an image with <code>MediaStore.ACTION_IMAGE_CAPTURE</code>.  I've got some kind of weirdness going on when I run the app on my phone.</p>
  
  <p>The camera app itself is flipping its orientation a couple times during operation even though I am not moving the phone.  It briefly goes into landscape mode before returning to the tutorial app.  Consequently, the tutorial app is flipping back to portrait mode after control is returned to it, and the image is lost.  I tried setting the camera activity's orientation to landscape, and the image is not lost.  </p>
  
  <p>But the layout of the app is intended for portrait mode.  Or, if I hold my camera in landscape orientation while capturing the photo, I can turn the phone after my app has returned to focus, and not lose the image.</p>
  
  <p>I did some poking around on the web.  Someone on Stackoverflow mentioned that the change in orientation caused additional calls to <code>onCreate</code>.  "The reason that <code>onCreate()</code> is called is because when you do call the camera activity during the portrait orientation, it will change the orientation and destroy your previous activity."  I ran the app in debugging mode with breakpoints set in onCreate and in the <code>onActivityResult</code> methods.  It is indeed true that <code>onCreate</code> is getting called when I take the photo in portrait mode.  The order of calls is onCreate, onActivityResult, onCreate.  If I take the photo in landscape mode (which is where my camera app ends up either way), onCreate does not get called.  Now that I have some idea what is going on, how do I keep that from being a problem?  Here's what the app looks like now:</p>
  
  <pre><code>package com.example.testapp;
  
  import java.io.IOException;
  import java.io.InputStream;
  
  import android.app.Activity;
  import android.app.WallpaperManager;
  import android.content.Intent;
  import android.content.res.Configuration;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.ImageButton;
  import android.widget.ImageView;
  
  public class CameraActivity extends Activity implements View.OnClickListener {
  
      ImageButton ib;
      Button b;
      ImageView iv;
      Intent i;
      final static int cameraData = 0;
      Bitmap bmp;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.photo_activity);
          initialize();
      }
  
      @Override
      public void onConfigurationChanged(Configuration newConfig) {
          // TODO Auto-generated method stub
          super.onConfigurationChanged(newConfig);
          setContentView(R.layout.photo_activity);
          initialize();
      }
  
      private void initialize() {
          iv = (ImageView)findViewById(R.id.imageViewReturnedPicture);
          ib = (ImageButton)findViewById(R.id.imageButtonTakePicture);
          b = (Button)findViewById(R.id.buttonSetWallpaper);
          b.setOnClickListener(this);
          ib.setOnClickListener(this);
      }
  
      @Override
      public void onClick(View arg0) {
          switch (arg0.getId()) {
  
          case R.id.buttonSetWallpaper:
              try {
                  WallpaperManager wm = WallpaperManager.getInstance(getApplicationContext());
                  wm.setBitmap(bmp);
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              break;
  
          case R.id.imageButtonTakePicture:
              i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
              startActivityForResult(i, cameraData);
              break;
          }
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          // TODO Auto-generated method stub
          super.onActivityResult(requestCode, resultCode, data);
          if (resultCode == RESULT_OK) {
              Bundle extras = data.getExtras();
              bmp = (Bitmap)extras.get("data");
              iv.setImageBitmap(bmp);
          }
      }
  }
  </code></pre>
  
  <p>And here's what I have in the manifest for this activity:</p>
  
  <pre>
                  android:name="com.example.testapp.CameraActivity"
                  android:label="Camera Activity"
                  android:configChanges="orientation"
                  android:screenOrientation="portrait" </pre>
  
  <p>I've done considerable searching, but much of what I find lacks concrete examples.  I need to know what the code looks like, not just what feature to use.</p>
  
  <p>My phone is an LG Motion.  Has anyone else run into this problem?  How can it be fixed?</p>