StartActivityForResult不会将数据返回到父活动

时间:2016-12-23 05:37:45

标签: android android-intent android-activity startactivityforresult

在活动A中有三个活动A,B,C,一个意图打开活动B.On活动B调用startActivityForResult并打开C.但是在C中调用setResult后它返回活动A而不是B 我正在复制我的活动代码,其中iam调用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();

            }
        }
    }
}

第二个活动cropActivity我将数据返回到父

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

请帮我解决这个问题,返回页面是另一项活动

2 个答案:

答案 0 :(得分:1)

希望您必须更正您的requestCode

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

因为你在这里提到过相同的

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

答案 1 :(得分:0)

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

或者 您可以按照以下解决方案(如果您愿意),而不是使用 startActivityForResult(),您可以使用 onNewIntent( )作为回调方法。

You need to follow few steps.

第1步:在AndroidManifest.xml中将“ CreateEventActivity ”的launchMode声明为“ singleTask

示例:

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

第2步:在“CreateEventActivity”中覆盖 onNewIntent()

示例:

 @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
             }
        }
    }

第3步:如何触发此回调     只需使用startActivity(),即startActivity(B,C)和Once'Activity C'完成startActivity(C,B)并在Activity C上调用finish()

示例

来自'CreateEventActivity'

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

来自'CropImageActivity '

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

注意:不要担心Activity B实例不会一次又一次地创建。如果您无法找到问题w.r.t startActivityForResult(),请尝试此解决方案。