我有一个班级,我有导航抽屉。 然后作为导航抽屉选项,我有一个片段,如果我们点击一个选项,则调用另一个片段替换当前片段。 在单击按钮的新片段中,我想从库中选择一个图像,然后将其显示在同一个片段中。
我已成功进入我们从图库中选择图像的部分,但在选择图像后,该应用程序将转到调用活动。
我正在使用startActivityForResult和onActivityResult。
private void selectImage() {
final CharSequence[] items = { "Take Photo", "Choose from Library", "Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals("Choose from Library")) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
SELECT_FILE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
Toast.makeText(this.getActivity().getBaseContext(), "Reached onActivityResult:Camera", Toast.LENGTH_LONG).show();
} else if (requestCode == SELECT_FILE) {
Toast.makeText(this.getActivity().getBaseContext(), "Reached onActivityResult:Gallery", Toast.LENGTH_LONG).show();
}
}
}
从对话框中选择一个选项然后选择图像后,将调用onActivityResult,但随后调用片段的托管活动并且视图更改并且图像不会保留
感谢任何帮助。
TIA
答案 0 :(得分:0)
您需要为所有未处理的请求代码致电super.onActivityResult()
。在Android Activity中,总是第一次通过onActivityResult()
处理结果。
你在片段中调用startActivityForResult()
。
请参阅此答案:onActivityResult is not being called in Fragment
我只是尝试过,它似乎对我有用。这是代码
片段
private void selectImage() {
final CharSequence[] items = {
"Take Photo", "Choose from Library", "Cancel"
};
AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals("Choose from Library")) {
Intent intent =
new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("onActivtyResult: ","Fragment");
super.onActivityResult(requestCode, resultCode, data);
}
的活动:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("onActivtyResult: ", "Activity");
super.onActivityResult(requestCode, resultCode, data);
}
logcat的
10-07 12:26:38.348 3781 3781 I onActivtyResult: : Activity
10-07 12:26:38.348 3781 3781 I onActivtyResult: : Fragment
答案 1 :(得分:0)
虽然我迟到了,但解决方案可能对其他人有用 问题在于生命周期方法。一旦我们调用隐式意图然后返回,则调用恢复上的活动。 如果您在 onResume 中添加了更新片段的代码,它将关闭您的片段。因此,相应地更改 onResume。