OnCreate is called after onActivityResult when I select a picture from the gallery

I have a "SherlockFragmentActivity" with an overridden "onActivityResult".

I am trying to get an image from a camera and gallery and crop it. The problem is that I returned my activity, not the fragment after calling onActivityResult.

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

Action Scheme:

 <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 a "SherlockFragment", where I selected the 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 'Sherlock Fragment':

 @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 the camera activity, my main activity call is "onSaveInstanceState" and after that "onRestoreInstanceState". This is the reason?

+6
source share
3 answers
  • Check the settings "Settings" → "Developers" → "Do not save actions."
  • This is the nature of the android, if your device requires memory, it destroys actions that are not visible. Therefore, you must consider that your activity can be recreated at any time. The BTW Do Not Hold Actions option exists to simulate your application when your device needs memory and destroys your backstack operations.
+9
source

Try it like this

Change

 Bitmap photo = extras.getParcelable("data"); 

For

 Bitmap photo=(Bitmap) intent.getExtras().get("data"); 

Edited: -

 @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"); Bitmap photo=(Bitmap) intent.getExtras().get("data");// Changed Here icon.setImageBitmap(photo); } if (requestCode == Const.GALLERY_PICTURE) { Log.d(LOG_TAG, "requestCode == GALLERY_PICTURE"); Uri imageUri= intent.getData();// Changed Here, or first decode the image to Avoid OutOfMemory Error icon.setImageURI(imageUri); } } 
0
source

Please replace

 frag.startActivityForResult(...); 

In your fragment

 startActivityForResult(...) 
0
source

Source: https://habr.com/ru/post/957057/


All Articles