startActivityForResult returns to the wrong activity

旧街凉风 提交于 2019-12-13 15:31:16

问题


I am working on an app which uses a custom camera (with Surfaceview and such), I am using startActivityForResult from my ObjectActivity to go to the activity with the camera named CameraActivity. This happens in this method.

public void addPicture(View v) {
    final CharSequence[] items = { "Take Photo", "Choose from Gallery", "Cancel" };
    AlertDialog.Builder builder = new AlertDialog.Builder(ObjectActivity.this);
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Take Photo")) {
                executeOnResume = false;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                    int hasWriteExternalStoragePermission = checkSelfPermission(Manifest.permission.CAMERA);
                    if (hasWriteExternalStoragePermission != PackageManager.PERMISSION_GRANTED) {
                        requestPermissions(new String[]{Manifest.permission.CAMERA},
                                REQUEST_CODE_ASK_PERMISSIONS);
                    }
                }
                Intent intent = new Intent(ObjectActivity.this,CameraActivity.class);
                startActivityForResult(intent, REQUEST_CAMERA);
            } else if (items[item].equals("Choose from Gallery")) {
                executeOnResume = false;
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/*");
                startActivityForResult(
                        Intent.createChooser(intent, "Select File"),
                        REQUEST_SELECT_FILE);
            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

To be more specific, it happens at these lines:

Intent intent = new Intent(ObjectActivity.this,CameraActivity.class);
startActivityForResult(intent, REQUEST_CAMERA);

This works pretty well, but when I try to return to ObjectActivity after taking a picture, which happens here:

Camera.PictureCallback mPicture = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        //TODO Code to process picture taken
        //create a new intent...
        Intent intent = new Intent();
        intent.putExtra("data",data);
        //close this Activity...
        setResult(Activity.RESULT_OK, intent);
        finish();
    }
};

It returns to the activity previous to the ObjectActivity named MainActivity, while it's supposed to go back to ObjectActivity and call onActivityResult() :

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    executeOnResume = false;
    loadStuff();
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_CAMERA || requestCode == REQUEST_SELECT_FILE) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
            alertDialog.setTitle("Description");
            alertDialog.setMessage("Enter Description");
            final EditText input = new EditText(this);
            alertDialog.setView(input);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            input.setLayoutParams(lp);
            alertDialog.setView(input);
            alertDialog.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            description = input.getText().toString();
                            if (description == null || description.equals("")) {
                                description = "-";
                            }
                            try {
                                savePhoto(requestCode,data);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });
            final AlertDialog.Builder tmpDialog = alertDialog;

            final AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
            dlgAlert.setTitle("Direction");
            dlgAlert.setMessage("Stand with your phone facing the same direction as the picture made and press Ok");
            dlgAlert.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            mBearingProvider.updateBearing();
                            bearing = mBearingProvider.getBearing();
                            cardinalDirection = bearingToString(bearing);
                            Log.e("Direction", cardinalDirection + "," + bearing);
                            tmpDialog.create().show();
                            dialog.dismiss();
                        }
                    });
            dlgAlert.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            tmpDialog.create().show();
                        }
                    });
            dlgAlert.create().show();
        }
    }
}

But it never gets there. Does anyone know why this happens?


回答1:


Get rid of that getParent. You want to set the result for the current activity, not your parent. So replace:

    //close this Activity...
    if (getParent() == null) {
        setResult(Activity.RESULT_OK, intent);
    } else {
        getParent().setResult(Activity.RESULT_OK, intent);
    }

with

setResult(Activity.RESULT_OK, intent);



回答2:


I've found the solution, if I don't send over a byte[] with the Intent (but instead maybe write out a file and send the path of that file with the Intent), the code seems to work and the ObjectActivity opens.

Part that I removed/changed:

intent.putExtra("data",data);

I don't know why this works, I'd like to know why, but for now I'm happy it works.



来源:https://stackoverflow.com/questions/35936171/startactivityforresult-returns-to-the-wrong-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!