Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3974672
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:35:42+00:00 2026-05-20T04:35:42+00:00

I read much about issues with onActivityResult , but it seems that none of

  • 0

I read much about issues with onActivityResult, but it seems that none of the described problems fit to mine, like putting a negative requestCode in startActivityForResult or something else.

I’m playing with the camera in my Activity, which streams its preview to a SurfaceView.
After taking a picture, I close the cam releasing its resources, call setResult(RESULT_OK, DataIntent) and hoping that onActivityResult is triggered in my parent.

But it doesn’t. If I set a result in onCreate of the child Activity and finish the child in onCreate the result is not passed to onActivityResult.

What possible reason could it be that onActivityResult is not getting triggered?
I’ll write some of my source down for understanding of what I’m doing…

public class MainActivity extends Activity {
    Button mButtonScan;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mButtonScan = (Button)findViewById(R.id.main_btn_scan);
    }

    /**
    * OnClick Event called from main.xml
    * @param v View that called that onClickEvent
    */
    public void btnCaptureClick(View v) {
        Intent intent = new Intent(this, CaptureActivity.class);
        startActivityForResult(intent, Constants.REQUEST_CODE_CAPTURE);
    }

    /**
    * callback for this Activity. Called when an Activity which was started by
    * this.startActivityForResult(intent, requestCode) sets its result and calls finish()
    */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String foo = "foo";
        switch (requestCode) {
            case Constants.REQUEST_CODE_CAPTURE:
                switch (resultCode) {
                    case RESULT_FIRST_USER:
                        Toast.makeText(this, data.getStringExtra(Config.SCAN_RESULT_TEXT), Toast.LENGTH_LONG).show();
                        break;
                    case RESULT_CANCELED:
                        break;
                    default:
                        break;
                }
                break;

            default:
                super.onActivityResult(requestCode, resultCode, data);
                break;
        }
    }
}


public class CaptureActivity extends Activity implements ActivityCallback, SurfaceHolder.Callback, PreviewCallback {

    private Preview mPreview;
    private Camera mCam;
    private SurfaceHolder mHolder;
    private Size size;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.capture);

        mPreview = (Preview)findViewById(R.id.capture_preview); 
    }

    @Override
    public void onValidDecodeResult(Result rawResult, Bitmap barcode) {
        Intent intent = new Intent();
        if (rawResult != null && barcode != null) {
            intent.putExtra(Config.SCAN_RESULT_TEXT, rawResult.getText());
            intent.putExtra(Config.SCAN_RESULT_FORMAT, rawResult.getBarcodeFormat().getName());
            intent.putExtra(Config.SCAN_RESULT_BMP, barcode);
        } else {
            intent.putExtra(Config.SCAN_RESULT_TEXT, "foo");
            intent.putExtra(Config.SCAN_RESULT_FORMAT, "bar");
            intent.putExtra(Config.SCAN_RESULT_BMP, "barcode");
        }
        mPreview = null;
        setResult(Activity.RESULT_FIRST_USER, intent);
        finish();   
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        MultiFormatReader reader = new MultiFormatReader();     
        PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, size.width, size.height, 160, 60, 480, 360);
        GlobalHistogramBinarizer binarizer = new GlobalHistogramBinarizer(source);
        BinaryBitmap bb = new BinaryBitmap(binarizer);
        Result result = null;
        try {
            result = reader.decode(bb);
        } catch (NotFoundException e) {
            //do NOTHING cause e == null
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            reader.reset();
        }
        if (result != null) {
            mCam.stopPreview();
            releaseCameraResources();
            onValidDecodeResult(result, source.renderCroppedGreyscaleBitmap());
        } else {
            camera.setOneShotPreviewCallback(this);
        }       
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCam = Camera.open();
            mCam.setPreviewDisplay(mPreview.getHolder());
        } catch (IOException e) {
            releaseCameraResources();
            e.printStackTrace();
        }           
    }

    private void releaseCameraResources() {
        mCam.release();
        mCam = null;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
        //begin Preview
        Camera.Parameters parameters = mCam.getParameters();

        List<Size> sizes = parameters.getSupportedPreviewSizes();

        size = getOptimalPreviewSize(sizes, width, height);
        parameters.setPreviewSize(size.width, size.height);

        mCam.setParameters(parameters);
        mCam.startPreview();
        mCam.setOneShotPreviewCallback(this);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (mCam != null) {
            mCam.stopPreview();
            releaseCameraResources();
        }   
    }

    private Size getOptimalPreviewSize(List<Size> sizes, int width, int height) {
        final double ASPECT_TOLERANCE = 0.05;
        double targetRatio = (double) width / height;

        if (sizes == null) return null; 
            Size optimalSize = null;
            double minDiff = Double.MAX_VALUE;

            int targetHeight = height;

            for (Size size: sizes) {
                double ratio = (double) size.width / size.height;
                if(Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) {
                    continue;
                }

                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }               
            }

            if (optimalSize == null) {
                //cannot find matching aspect-ratio
                minDiff = Double.MAX_VALUE;
                for (Size size : sizes) {
                    if (Math.abs(size.height - targetHeight) < minDiff) {
                        optimalSize = size;
                        minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:35:43+00:00Added an answer on May 20, 2026 at 4:35 am

    I solved the problem myself now.

    After giving up to get onActivityResult triggering, I decided to ‘hack’ Androids encapsulating by passing a static reference from MainActivity to CaptureActivity, even if I know that this isn’t a good idea.

    After the finish() call, MAGICALLY onActivityResult gets triggered with Context.RESULT_CANCELLED… as expected because I don’t call setResult anymore.

    Getting onActivityResult triggered I investigated why it is working now. I found out, that it has something to do with the bitmap, passed to an Intent. If I put a Parcellable Bitmap into my resultIntent onActivityResult never gets fired.

    So removing following line in the code above will work:

    intent.putExtra(Config.SCAN_RESULT_BMP, barcode);
    

    Thats ok for me, because I don’t needed the BitMap really in this other Activity. It was more a ‘feature’ than a need.

    If some of you guys want to pass big Data into an Intent like a Bitmap, think about storing it somewhere on SD, passing the path in the Intent, while reading this Bitmap from SD in ParentActivity (How to take a photo by Intent).

    As stated here, the Limit of a Parcelable is 1MB, thus everything bigger passed to an Intent will cause an internal TransactionTooLargeException and will silently fail

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I like a lot of what I've read about D. Unified Documentation (That would
I have read the documentation for the CI Caching but still don't understand much
I've looked at this but it wasn't too much help. Maybe I didn't read
One thing that really interests me that I don’t see much written about or
There are some discussions about the same question but I would like to ask
for a FILE* stream, if I read as much data as possible, feof(stream) returns
I read this PHP RegEx page , but either I'm missing something, misreading something,
I read somewhere once that the modulus operator is inefficient on small embedded devices
I read in this article that a company has created a software capable of
I've read a lot about the pros and cons of sizing with either relative

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.