Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android app losing data during orientation change

I've got an app that I've copied from a tutorial that captures an image with MediaStore.ACTION_IMAGE_CAPTURE. I've got some kind of weirdness going on when I run the app on my phone.

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.

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.

I did some poking around on the web. Someone on Stackoverflow mentioned that the change in orientation caused additional calls to onCreate. "The reason that onCreate() 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 onActivityResult methods. It is indeed true that onCreate 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:

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);
        }
    }
}

And here's what I have in the manifest for this activity:

                android:name="com.example.testapp.CameraActivity"
                android:label="Camera Activity"
                android:configChanges="orientation"
                android:screenOrientation="portrait" 

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.

My phone is an LG Motion. Has anyone else run into this problem? How can it be fixed?

like image 266
Roxann Higuera Avatar asked Feb 23 '13 17:02

Roxann Higuera


1 Answers

In the manifest , In each activity I use configChanges:

 <activity
        android:name=".MainActivity"
        android:configChanges="screenLayout|orientation|screenSize">

I hope that this help you.

like image 114
Cabezas Avatar answered Sep 23 '22 00:09

Cabezas