why is UI/Activity destroyed after onActivityResul

2019-07-24 17:38发布

问题:

I'm trying to update the imageview on onActivityResult after I call up a camera intent but it seems like the view is getting destroyed and recreated after the onActivityResult. It works fine if I do it on the onCreate. I'm using a Galaxy S3 for testing.

Oddly it seems to have worked for a little while when I built the same code on my desktop, but not on my laptop. After debugging it for a while the problem arose again in the desktop build as well and in the debugger the bitmap gets updated correctly in the onactivityresult but then the activity gets destroyed and created immediately again resetting the view to the initial state.

The onCreate is called three times, 1. when the screen first shows up, 2. after the camera intent is finished and before onActivityResult, and 3. after the on Activity Result. I'm not sure why the activity gets destroyed and re-created before #2 and #3.

I'm not doing any orientation changes.

** Code **

Acvity settings in manifest

   android:configChanges="orientation"
   android:screenOrientation="portrait" 

Activity

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        if (resultCode == RESULT_OK) {
            String imageUri = "file:///storage/sdcard0/Pictures/Funhouse/IMG_20130826_163938.jpg";

            // Try by using setImageURI
            preview.setImageURI(Uri.parse(imageUri));

        }
     }

The onCreate and Intent request

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        preview = (ImageView) findViewById(R.id.img_preview);

//      works fine if I set the image view here
//      String imageUri = "file:///storage/sdcard0/Pictures/Funhouse/IMG_20130826_163938.jpg";
//      preview.setImageURI(Uri.parse(imageUri));
    }



    public void takePhoto(View view) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 0);

    }

Here is the layout file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical" >

     <Button
         android:id="@+id/btn_take_photo"
         android:layout_width="match_parent"
         android:layout_height="50dp"
         android:layout_marginBottom="67dp"
         android:onClick="takePhoto"
         android:text="Take Photo" />

    <ImageView
        android:id="@+id/img_preview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

 </LinearLayout>

回答1:

So it turns out returning from the camera activity was calling the on destroy after the ui was updated in OnActivityResult, resetting the UI of the original calling activity. The screen resolution change was being triggered by the Camera Activity returning, but only after the onActivityResult event.

Updating the android manifest to ignore screen resolution changes on that activity fixed it.

 android:configChanges="orientation|screenSize"
 android:screenOrientation="portrait"