Why is the oncreate method called after startActivityForResult?

In my application, I want to select an image from Gallery and set the image to a ListView . In my call to ListView I have 16 rows. So when ever item click in I have 16 rows. So when ever item click in ListView open the gallery and set the image to ListView . But my problem is some times after . But my problem is some times after startActivityForResult () , oncreate () `. This is the onclick code

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

And this is 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; 

}}

In this ListView after selecting 6 or 7 images called by the oncreate method, before onactivityresult() . But after the oncreate() method called the startactivity result again. Please tell me what the problem is. Thanks to InAdvance everyone ..

+5
source share
2 answers

before onactivityresult (). But after the oncreate () method starts to act again, we get

when an activity is sent to the background (when another activity gets on top of it or when it is sent to the background with a home button), its instance is stored in memory until the system is under pressure from the memory. when there is not enough memory in the system to do what it is doing in the foreground now, it usually repeatedly requests the memory, stopping and freeing the memory from background activity.

in this case, the system will give you an Activity.onSaveInstanceState callback that will be called from the activity that will be killed before it will give you the opportunity to save any state necessary for salvation before it is killed.

when your activity returns to the forefront - it will be recreated (which is called onCreate () again), with the savedInstanceState parameter which will not be zero.

the savedInstanceState file will contain a package with all the additional functions specified in the onSavedInstanceState () callback.

This is very important to understand.

for a better understanding, I advise you to seriously read - http://developer.android.com/training/basics/activity-lifecycle/recreating.html

+7
source

Perhaps the selection dialog is too large for the system, and it reduces memory by caching the application. Check the onSaveInstanceState method, which is called before onCreate . If so, then you can save the necessary data in the kit and load it into the onCreate method

0
source

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


All Articles