【问题标题】:Why oncreate method called after startActivityForResult?为什么在 startActivityForResult 之后调用 oncreate 方法?
【发布时间】:2014-10-14 10:57:20
【问题描述】:

在我的应用程序中,我想从Gallery 中选择图像并将图像设置在ListView 中。在我的 ListViewI have 16 rows. So when ever item click inListViewopen the gallery and set the image toListView. But my problem is some times afterstartActivityForResult(),oncreate()` 方法中调用。这是onclick代码

Intent intent = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    startActivityForResult(
                            Intent.createChooser(intent, "Select File"),
                            SELECT_PICTURE);

这是 onactivityresult

public void onActivityResult(int requestcode, int resultcode, Intent data) {
    Log.e("result", "result");
    displayMetrics = this.getResources().getDisplayMetrics();
    Ew = displayMetrics.widthPixels;
    Eh = displayMetrics.heightPixels;

    switch (requestcode) {
    case SELECT_PICTURE:
        if (resultcode == RESULT_OK) {

            lelListLayout.setVisibility(View.GONE);
            relImageLayout.setVisibility(View.VISIBLE);


            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);

            ExifInterface exif = null;
            // Bitmap bmRotated = null;

            try {
                exif = new ExifInterface(selectedImagePath);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_UNDEFINED);
            Log.e("Orientation==>", "" + orientation);

            try {

                bmRotated = null;
                bmGallayImage = null;
                trimCache();
                bmGallayImage = convertBitmap(selectedImagePath);
                bmRotated = InventorySubmitImagesActivity.rotateBitmap(
                        bmGallayImage, orientation);

                // if(bmRotated.getWidth()>bmRotated.getHeight()){
                if (bmRotated.getWidth() > 1024) {
                    float x = 0;
                    x = 1024 / (float) bmRotated.getWidth();
                    // Log.e("x====","value "+x);

                    bmRotated = Bitmap.createScaledBitmap(bmRotated, 1024,
                            (int) (bmRotated.getHeight() * x), true);
                }
                /*
                 * }else{ if(bmRotated.getHeight() > 1024){ float x=0;
                 * x=1024/(float)bmRotated.getHeight();
                 * Log.e("x====","value "+x);
                 * 
                 * bmRotated = Bitmap.createScaledBitmap(bmRotated,
                 * (int)(bmRotated.getWidth()*x), 1024, true); } }
                 */


                Eh = Eh - ll_buttonlayout.getHeight();


                float iw = bmRotated.getWidth();
                float ih = bmRotated.getHeight();



                float diff = Ew / iw;

                float layoutwidth = Ew;
                float layoutheight = diff * ih;

                if (layoutheight > Eh) {

                    diff = Eh / ih;
                    layoutwidth = Ew * diff;
                    layoutheight = Eh;
                }

                bmGallayImage = bmRotated;
                if (bmRotated != null) {

                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                            (int) layoutwidth, (int) layoutheight);
                    relImage.setLayoutParams(layoutParams);


                    Drawable dr = new BitmapDrawable(bmRotated);
                    old_width = bmRotated.getWidth();
                    old_height = bmRotated.getHeight();

                    relImage.setBackgroundDrawable(dr);

                }
                left = (int) layoutwidth / 2 - 34;
                top = (int) layoutheight / 2 - 8;

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // drag_check=true;
            relImage.removeAllViews();

            imgMarker = new ImageView(this);
            final RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.MATCH_PARENT);
            relImage.addView(imgMarker, layoutParams);
            imgMarker.setScaleType(ImageView.ScaleType.MATRIX);
            bmdragImage = BitmapFactory.decodeResource(getResources(),
                    R.drawable.image_marker);
            imgMarker.setImageBitmap(bmdragImage);

            matrix = new Matrix();
            savedMatrix = new Matrix();
            oldDist = 0f;
            start = new PointF();
            mid = new PointF();

            matrix.postTranslate(left, top);
            imgMarker.setImageMatrix(matrix);

            imgMarker.setOnTouchListener(RefurbishmentImageActivity.this);
            imgMarker.setVisibility(View.VISIBLE);

            // end..
            // }

        }
        break;

} }

在这个ListView 选择6 或7 个图像后调用oncreate 方法,在onactivityresult() 之前。但是在oncreate() 方法之后再次调用了startactivity 结果。请指导我是什么问题。感谢 InAdvance 所有人..

【问题讨论】:

    标签: android image-gallery


    【解决方案1】:

    在 onactivityresult() 之前。但是在 oncreate() 方法之后再次调用 startactivity 结果

    当 Activity 被发送到后台时(当其他 Activity 位于它之上时,或者当它被主页按钮发送到后台时)只要系统没有内存压力,它的实例就会保持活动状态。当系统没有足够的内存来做它当前在前台做的任何事情时,它通常会通过停止和释放内存后台活动来重新声明内存。

    在这种情况下 - 系统会为您提供 Activity.onSaveInstanceState 回调,该回调将从将要被终止的活动中调用,以便您有机会在它被终止之前保存任何需要保存的状态。

    当您的活动返回前台时 - 它将被重新创建(这就是再次调用 onCreate() 的原因),且 savedInstanceState 参数不会为空。

    savedInstanceState 将包含您在 onSavedInstanceState() 回调中提供的所有附加功能。

    理解这一点非常重要。

    为了更好的理解,我建议你认真阅读-http://developer.android.com/training/basics/activity-lifecycle/recreating.html

    【讨论】:

    • 我是一位经验丰富的开发人员,但我从未想过 startActivityForResult 的这个问题。但是当我遇到问题时,它是如此明显。
    • 天哪!甚至不知道这存在,我已经做了大约 6 年的 Android 开发。我只是被这个烧伤了,因为正在测试的平板电脑已经有一段时间没有重新启动了。我试图使用ACTION_IMAGE_CAPTURE 拍摄照片,这正在杀死活动并导致我的所有实例变量被清除。最棘手的部分是,在使用ACTION_VIDEO_CAPTURE 捕获视频或从图库导入视频时,它并没有这样做。只是拍照 - 可能是因为 Stock Photo 应用程序有更多控件并且需要更多内存。重启就好了!
    【解决方案2】:

    可能是带有选择器的对话框对系统来说太大了,它会通过缓存您的应用程序来减少内存。检查是在onCreate 之前调用的方法onSaveInstanceState。如果是,那么您可以将所需的数据保存在 bundle 中并在onCreate 方法中加载

    【讨论】:

    • 这是内存问题还是其他问题?
    • 这可能是图像选择器造成的 - 你启动它 6-7 次,他分配了大量内存,然后系统需要调用内存清理工具
    • 解决这个问题的最好方法是实现 saveInstanceState 机制,因为你不能触摸选择器 - 用户可能会使用另一个坏的选择器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    相关资源
    最近更新 更多