2

在活动 A 中有三个活动 A、B、C,意图打开活动 B。在活动 B 上调用 startActivityForResult 并打开 C。但是在 C 中调用 setResult 后,它返回到活动 A 而不是 B 我正在复制我的活动代码我正在调用开始活动创建事件

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

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

4

2 回答 2

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);
于 2016-12-23T05:46:50.977 回答
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),一旦 'Activity C' 工作完成 startActivity(C,B) 并在 Activity C 上调用 finish()

例子

来自“创建事件活动”

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

来自“CropImageActivity

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

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

于 2016-12-23T08:13:06.073 回答