当我从图库中选择图片时,在onActivityResult之后调用的onCreate [英] onCreate called after onActivityResult when I pick picture from gallery

查看:123
本文介绍了当我从图库中选择图片时,在onActivityResult之后调用的onCreate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有覆盖了 onActivityResult的 SherlockFragmentActivity。

I have 'SherlockFragmentActivity' with overrided 'onActivityResult'.

我尝试从相机和图库中获取图像并进行裁剪。
问题是在调用onActivityResult之后我返回了我的活动而不是片段。

I try to get image from camera and gallery and crop it. The problem is I returned on my activity not fragment after onActivityResult called.

...
            FragmentTransaction t = fragmentManager.beginTransaction();
            LogInFragment logFrag = new LogInFragment();
            t.replace(R.id.fragment_container, logFrag);
            t.commit();
...
     @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
        }

活动布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/fragment_container"
                android:background="@color/textWhite"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

</RelativeLayout>

我也有'SherlockFragment',我在其中选择了图像:

And I also have 'SherlockFragment' where I picked image:

startImagePickerDialog(this);


public void startImagePickerDialog() {
        AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(getSherlockActivity());
        myAlertDialog.setTitle("Upload Pictures Option");
        myAlertDialog.setMessage("How do you want to set your picture?");

        myAlertDialog.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                Intent intent = new Intent();
                // call android default gallery
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                // ******** code for crop image
                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 1);
                intent.putExtra("aspectY", 1);
                intent.putExtra("outputX", 200);
                intent.putExtra("outputY", 200);
                intent.putExtra("noFaceDetection", true);
                try {
                    intent.putExtra("return-data", true);
                    startActivityForResult(Intent.createChooser(intent,
                            "Complete action using"), Const.GALLERY_PICTURE);
                } catch (ActivityNotFoundException e) {
                    Log.e(LOG_TAG, "ActivityNotFoundException");
                }
            }
        });
        myAlertDialog.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                // call android default camera
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 1);
                intent.putExtra("aspectY", 1);
                intent.putExtra("outputX", 200);
                intent.putExtra("outputY", 200);
                intent.putExtra("noFaceDetection", true);
                try {
                    intent.putExtra("return-data", true);
                    startActivityForResult(intent, Const.CAMERA_REQUEST);
                } catch (ActivityNotFoundException e) {
                    Log.e(LOG_TAG, "ActivityNotFoundException");
                }
            }
        });
        myAlertDialog.show();
    }

SherlockFragment中的 onActivityResult:

And 'onActivityResult' in 'SherlockFragment':

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(LOG_TAG, "onActivityResult");
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != getSherlockActivity().RESULT_OK) {
            Log.e(LOG_TAG, "resultCode != RESULT_OK");
            return;
        }

        if (requestCode == Const.CAMERA_REQUEST) {
            Log.d(LOG_TAG, "requestCode == CAMERA_REQUEST");
            Bundle extras = data.getExtras();
            if (extras != null) {
                Log.d(LOG_TAG, "extras != null");
                Bitmap photo = extras.getParcelable("data");
                icon.setImageBitmap(photo);
            }
        }

        if (requestCode == Const.GALLERY_PICTURE) {
            Log.d(LOG_TAG, "requestCode == GALLERY_PICTURE");
            Bundle extras2 = data.getExtras();
            if (extras2 != null) {
                Log.d(LOG_TAG, "extras != null");
                Bitmap photo = extras2.getParcelable("data");
                icon.setImageBitmap(photo);
            }
        }
    }

更新
当我调用相机活动时,我的主要活动称为 onSaveInstanceState,之后称为 onRestoreInstanceState。

UPDATE When I call camera activity my main activity call 'onSaveInstanceState' and after that 'onRestoreInstanceState'. Is it a reason?

推荐答案


  1. 检查设置->开发人员选项-> 不保留活动标志。

  2. 这是android的本质,如果您的设备需要内存,则会破坏不可见的活动。因此,您必须考虑可以随时重新创建您的活动。 BTW的不保留活动选项可以在设备需要内存并破坏后台活动时模拟您的应用程序。

这篇关于当我从图库中选择图片时,在onActivityResult之后调用的onCreate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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