Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onCreate called after onActivityResult when I pick picture from gallery

I have 'SherlockFragmentActivity' with overrided '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);
        }

Activity layout:

<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>

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

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

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

like image 679
Val Avatar asked Oct 30 '13 16:10

Val


People also ask

How does onActivityResult work on Android?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.

How can we call fragment onActivityResult from activity?

To get the result in your fragment make sure you call startActivityForResult(intent,111); instead of getActivity(). startActivityForResult(intent,111); inside your fragment.

What is onActivityResult?

onActivityResult is the callback you have on the first activity to grab the contacts you choose. Follow this answer to receive notifications.

What is Requestcode in startActivityForResult?

The request code identifies the return result when the result arrives. ( You can call startActivityForResult more than once before you get any results.


1 Answers

  1. Check your "Settings" -> "Developer options" -> "Don't keep activities" flag.
  2. This is the nature of android if your device needs memory it destroys activities which are not visible. So you have to consider that your activity can be recreated any time. BTW "Don't keep activities" option is there to simulate your application when your device needs memory and destroys your backstack activities.
like image 139
Devrim Avatar answered Sep 30 '22 18:09

Devrim