为什么UI /活动的onActivityResult后销毁 [英] why is UI/Activity destroyed after onActivityResult

查看:152
本文介绍了为什么UI /活动的onActivityResult后销毁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想后,我打电话好了摄像头意图更新的onActivityResult 的ImageView的,但它似乎像视图得到破坏,之后的onActivityResult重新创建。如果我这样做是对的onCreate它工作正常。我使用的是银河S3进行测试。

奇怪的是它似乎已经工作了一会儿,当我建立在同一code我的桌面上,但不能在我的笔记本电脑。调试了一段时间后,这个问题在桌面版本再次出现,以及与在调试器中的位图获取的的onActivityResult正确更新但随后的活性被破坏,并产生立即的观点再次重置为初始状态。

本的onCreate是在活动后的结果称为三次,1。当屏幕首先显示了,2相机意图结束后和之前的onActivityResult和3。我不知道为什么活性被破坏,#2,#3,之前重新创建

我没有做任何方向的变化。

** code **

在清单Acvity设置

 的android:configChanges =方向
   机器人:screenOrientation =画像

活动

  @覆盖
    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
        如果(结果code == RESULT_OK){
            字符串imageUri =文件:///storage/sdcard0/Pictures/Funhouse/IMG_20130826_163938.jpg;            //使用setImageURI尝试
            preview.setImageURI(Uri.parse(imageUri));        }
     }

本的onCreate和意图的要求

  @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        preVIEW =(ImageView的)findViewById(R.id.img_ preVIEW);//如果我这里设置的图像显示工作正常
//字符串imageUri =文件:///storage/sdcard0/Pictures/Funhouse/IMG_20130826_163938.jpg;
// preview.setImageURI(Uri.parse(imageUri));
    }    公共无效takePhoto(查看视图){
        意向意图=新意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
        startActivityForResult(意向,0);    }

下面是布局文件

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
     机器人:layout_width =FILL_PARENT
     机器人:layout_height =FILL_PARENT
     机器人:方向=垂直>     <按钮
         机器人:ID =@ + ID / btn_take_photo
         机器人:layout_width =match_parent
         机器人:layout_height =50dp
         机器人:layout_marginBottom =67dp
         安卓的onClick =takePhoto
         机器人:文字=拍摄照片/>    < ImageView的
        机器人:ID =@ + ID / IMG_ preVIEW
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:SRC =@绘制/ ic_launcher/> < / LinearLayout中>


解决方案

因此​​,原来从摄像机活动返回被调用UI在更新的onActivityResult后的破坏,重置原始调用活动的UI。屏幕分辨率更改是由摄像头活动返回被触发,但只有活动的onActivityResult后。

更新Android清单忽略该活动固定它的屏幕分辨率变化。

 的android:configChanges =方向|屏幕尺寸
 机器人:screenOrientation =画像

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>

解决方案

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" 

这篇关于为什么UI /活动的onActivityResult后销毁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆