StartActivityForResult is not returning data to parent activity

1.6k views Asked by At

have three Activity A,B,C, in Activity A an intent opens Activity B.On activity B startActivityForResult is called and opens C. but after calling setResult in C it is returning to Activity A not to B i am copying my activity code where iam calling start activity create event

     @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SELECT_FILE) {
        if (resultCode == Activity.RESULT_OK) {

            alertDialogBox.dismiss();
            Intent image = new Intent(CreateEventActivity.this, CropImageActivity.class);
            image.putExtra("data", data.getData().toString());
            startActivityForResult(image, 2);

        }

    }
    if (requestCode == 2) {
        if (resultCode == Activity.RESULT_OK) {
            String bt = data.getStringExtra("result");
            file_path = data.getStringExtra("filepath");
            testPath = new File(file_path);
            Log.e("desti", testPath.toString());
            Log.e("destpat", testPath.getAbsolutePath());
            try {
                testPath.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                byte[] encodeByte = Base64.decode(bt, Base64.DEFAULT);
                bitmap2 = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);

                //  imageforUpload = getStringImage(bitmap2);
                BitmapFactory.Options options = new BitmapFactory.Options();

                // down sizing image as it throws OutOfMemory Exception for larger
                // images
                options.inSampleSize = 8;

                final Bitmap bitmaptest = BitmapFactory.decodeFile(file_path, options);

                imgCropped.setVisibility(View.VISIBLE);
                imgCropped.setImageBitmap(bitmaptest);


            } catch (Exception e) {
                e.getMessage();

            }
        }
    }
}

second activity cropActivity where i am returning data to parent

     private final CropCallback mCropCallback = new CropCallback() {
    @Override
    public void onSuccess(Bitmap cropped) {
        int height = cropped.getHeight();
        int width = cropped.getWidth();
        if (height<400||width<400){
            Toast.makeText(getApplicationContext(),"this image cat be cropped",Toast.LENGTH_SHORT).show();
        }else {

            Bitmap bitmap = Bitmap.createBitmap(cropped, 0, 0, 400, 400);


            imgtest.setImageBitmap(bitmap);
            SaveImage(bitmap);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            byte[] b = baos.toByteArray();
            String temp = Base64.encodeToString(b, Base64.DEFAULT);


            Intent returnIntent = new Intent();
            returnIntent.putExtra("result", temp);
            returnIntent.putExtra("filepath", filePath);
            setResult(Activity.RESULT_OK, returnIntent);
            finish();
        }
    }

please help me to solve this ,the returning page is another activity

2

There are 2 answers

0
A.R. On
First a fall you need to check the flow of control of your code, whether its going in correct blocks or not.

Alternatively you can follow below solution(If you wish), instead of using startActivityForResult() you can use onNewIntent() as a callback method.

You need to follow few steps.

Step 1: Declare launchMode of your 'CreateEventActivity' as "singleTask" in your AndroidManifest.xml

Example:

 <activity android:name=".CreateEventActivity" android:launchMode="singleTask"/>

Step 2: Override onNewIntent() in your 'CreateEventActivity'

Example:

 @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if(intent!=null)
        {
            // YOU CAN CHECK FLAGS OVER HERE, BY PASSING THE VALUES THROUGH INTENTS from different other activities
             if(intent.getBooleanExtra("IS_COMING_FROM_CROP_ACTIVITY",false)
             {
                // HANDLE YOUR STUFF
             }
        }
    }

Step 3: How to trigger this callback Simply use startActivity(), that is startActivity(B,C) and Once 'Activity C' works done startActivity(C,B) and call finish() on Activity C

Example

From 'CreateEventActivity'

startActivity(new Intent(CreateEventActivity.this, CropImageActivity.class));

From 'CropImageActivity'

startActivity(new Intent(CropImageActivity.this, CreateEventActivity.class));
finish();

Note: Don't worry Activity B instance won't create again and again. Try this solution if you not been able to find the issue w.r.t startActivityForResult().

2
Sridhar On

Hope you have to correct your requestCode

Intent returnIntent = new Intent();
returnIntent.putExtra("result", temp);
returnIntent.putExtra("filepath", filePath);
setResult(2, returnIntent);
finish();

Because you have mentioned the same here

Intent image = new Intent(CreateEventActivity.this, CropImageActivity.class);
image.putExtra("data", data.getData().toString());
startActivityForResult(image, 2);