【问题标题】:StartActivityForResult is not returning data to parent activityStartActivityForResult 没有将数据返回给父活动
【发布时间】:2017-05-08 19:45:18
【问题描述】:

有三个活动A,B,C,在活动A中,一个意图打开活动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();
        }
    }

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

【问题讨论】:

  • 你在调用Activity C之前完成Activity B了吗?
  • @sohanshetty no
  • 请提供类名,这样从哪个类调用哪个类就清楚了
  • @JoseAntony 当您使用 StartActivityForResult 开始一个活动时,它来自哪个活动?因为我看到你已经在 OnActivityResult 中实现了代码
  • 确保在执行 startActivityForResult(yourIntent, SELECT_FILE); 之前或之后没有调用 finish();

标签: android android-intent android-activity startactivityforresult


【解决方案1】:

希望您必须更正您的请求代码

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

【讨论】:

  • 我已经更改了请求代码,但它还没有工作
  • @JoseAntony 尝试调试并确认它是否进入onActivityResult?
【解决方案2】:
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()

示例

来自“CreateEventActivity”

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

来自“CropImageActivity

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

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

【讨论】:

    猜你喜欢
    • 2016-06-26
    • 1970-01-01
    • 2021-10-09
    • 2012-10-17
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 2014-01-10
    • 2016-08-21
    相关资源
    最近更新 更多