Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why oncreate method called after startActivityForResult?

In my application i want to select image from Gallery and set the image in ListView. In my 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()` method called. 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 the 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 oncreate method called, before the onactivityresult(). But after oncreate() method again startactivity result called. Please guide me what is the problem. Thanks InAdvance to all..

like image 480
rams Avatar asked Oct 14 '14 10:10

rams


People also ask

Why onCreate is called twice?

onCreate will get called when your activity has been destroyed and recreated, which happens any time the device is rotated, the keyboard is opened, or you switch apps and the system decides it's time to reclaim some memory and kill off your app.

What does onCreate mean?

Android App Development for Beginners onCreate() is called when the when the activity is first created. onStart() is called when the activity is becoming visible to the user.

Is onCreate only called once?

OnCreate is only called once.

What is used of onCreate () method?

onCreate(savedInstanceState); calls the method in the superclass and saved InstanceState of the activity if any thing damage the activity so its saved in instanceState so when reload the activity it will be the same before. Save this answer.


2 Answers

before the onactivityresult(). But after oncreate() method again startactivity result called

when Activity is sent to background (when other activity becomes on top of it, or when it is sent by the home button to background) its instance is kept alive as long as the system is not under memory pressure. when the system doesn't have enough memory to do whatever it's currently doing in foreground, it usually will re-claim memory by stopping and releasing from memory background activities.

in that case - the system provide you with the Activity.onSaveInstanceState callback which will be invoked from the activity that's going to be killed to provide you a chance to save any state needed to be saved before it's being killed.

when your activity will return to foreground - it will be re-created (that's why onCreate() called again), with savedInstanceState parameter that will not be null.

the savedInstanceState will hold bundle with all the extras you provided in the onSavedInstanceState() callback.

this is very important to understand.

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

like image 89
Tal Kanel Avatar answered Oct 06 '22 00:10

Tal Kanel


May be dialog with selection chooser is too huge for the system and it reduces memory by caching your application. Check is method onSaveInstanceState called before onCreate. If yes, then you can save needed data in bundle and load it in onCreate method

like image 45
Artem Mostyaev Avatar answered Oct 06 '22 01:10

Artem Mostyaev