startActivityForResult 返回错误的活动
Posted
技术标签:
【中文标题】startActivityForResult 返回错误的活动【英文标题】:startActivityForResult returns to the wrong activity 【发布时间】:2016-06-26 11:01:20 【问题描述】:我正在开发一个使用自定义相机(使用 Surfaceview 等)的应用程序,我正在使用我的 ObjectActivity 中的 startActivityForResult 来使用名为 CameraActivity 的相机进行活动。这发生在这个方法中。
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();
更具体地说,它发生在以下几行:
Intent intent = new Intent(ObjectActivity.this,CameraActivity.class);
startActivityForResult(intent, REQUEST_CAMERA);
这很好用,但是当我在拍照后尝试返回 ObjectActivity 时,发生在这里:
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();
;
它返回到名为MainActivity的ObjectActivity之前的Activity,而它应该回到ObjectActivity并调用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();
但它永远不会到达那里。 有谁知道为什么会这样?
【问题讨论】:
你是否在使用 GroupActivity? ObjectActivity 和 CameraActivity 都是常规活动,而 MainActivity 是 AppCompatActivity,据我所知,我没有使用 GroupActivity 【参考方案1】:摆脱那个getParent。您想为当前活动设置结果,而不是您的父母。所以替换:
//close this Activity...
if (getParent() == null)
setResult(Activity.RESULT_OK, intent);
else
getParent().setResult(Activity.RESULT_OK, intent);
与
setResult(Activity.RESULT_OK, intent);
【讨论】:
我想我已经尝试过了,但我会再试一次 设置父母的结果是绝对100%错误的。 这可能是真的,但这不是解决方案,我得到了相同的结果【参考方案2】:我找到了解决方案,如果我不使用 Intent 发送一个字节 [](而是可能写出一个文件并使用 Intent 发送该文件的路径),代码似乎可以工作并且ObjectActivity 打开。
我删除/更改的部分:
intent.putExtra("data",data);
我不知道为什么会这样,我想知道为什么,但现在我很高兴它有效。
【讨论】:
以上是关于startActivityForResult 返回错误的活动的主要内容,如果未能解决你的问题,请参考以下文章
如何从 TabHost Activity 返回结果(startActivityForResult)?
如何从 TabHost Activity 返回结果(startActivityForResult)?
startActivityForResult 返回错误的活动
在 startActivityForResult 返回后,Android parentActivity 没有重新创建