gpt4 book ai didi

java - 为什么我没有收到 onActivityResult 的任何结果

转载 作者:行者123 更新时间:2023-12-01 09:48:46 25 4
gpt4 key购买 nike

我通过在按下按钮时调用 showPickImageDialog() 方法从 AddFragment 启动 DialogFragment 。在 AlertDialogFragment onActivityResult 中,我将结果发送回 AddFragment,但 AddFragment 没有收到任何结果。有什么想法吗?

添加 fragment :

    public class AddFragment extends Fragment {

private static final int REQUEST_IMAGE = 3;
private static final String DIALOG_IMAGE = "image";
private static final String TAG = "TAG";

void showPickImageDialog() {
FragmentManager fm = getActivity()
.getSupportFragmentManager();
AlertDialogFragment pdialog = new AlertDialogFragment();
pdialog.setTargetFragment(this, REQUEST_IMAGE);
pdialog.show(fm, DIALOG_IMAGE);
}



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);


if (requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK){

Log.e(TAG,"back in AddFragment");

}
}
}

在 AlertDialogFragment 中,我从 onActivityResult 方法调用 sendResult 方法。 SendResult 方法被调用我用日志检查了它

AlertDialogFragment:

public class AlertDialogFragment extends DialogFragment {

private final static String TAG = "Cload";
public static final String EXTRA_PHOTOFILENAME =
"com.example.tadas.dreamcload1.image";
private static final int REQUEST_IMAGE = 3;
private String filename;
private Uri uri;


private void sendResult(int resultCode) {
if(getTargetFragment() == null){return;}

Intent i = new Intent();
i.putExtra(EXTRA_PHOTOFILENAME,filename);
getTargetFragment()
.onActivityResult(getTargetRequestCode(), resultCode, i);

}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

//LayoutInflater inflater = getLayoutInflater();
// View dialogLaout = inflater.inflate(R.layout.custom_dialog_pick_image,null);
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.custom_dialog_pick_image, null);
final AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setView(view)
.create();
ImageButton gallerybtn = (ImageButton)view.findViewById(R.id.gallery);
ImageButton camerabtn = (ImageButton)view.findViewById(R.id.camera);

gallerybtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(TAG,"gallery btn pressed");
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_IMAGE);
}
});
camerabtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});


return dialog;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK){

uri = data.getData();

final String[] filePathColumn = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME};
Cursor c = getActivity().getContentResolver()
.query(uri,filePathColumn,null,null,null);

if (c.moveToFirst()){
int ids = c.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
uri = Uri.parse(c.getString(ids));
filename = uri.toString();
c.close();
}
sendResult(Activity.RESULT_OK);
dismiss();
}

}

最佳答案

您可以在 Activity 中重写 onActivityResult 方法,并手动调用 Fragment 的 onActivityResult,如下所示:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.your_container);
fragment.onActivityResult(requestCode, resultCode, data);
}

您可以在 Activity 的 onActivityResult 方法中定义此部分:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK){

uri = data.getData();

final String[] filePathColumn = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME};
Cursor c = getActivity().getContentResolver()
.query(uri,filePathColumn,null,null,null);

if (c.moveToFirst()){
int ids = c.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
uri = Uri.parse(c.getString(ids));
filename = uri.toString();
c.close();
}
// You do not need this any more
//sendResult(Activity.RESULT_OK);
//dismiss();
// Just find your fragment and call it's onActivityResult
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.your_container);
fragment.onActivityResult(requestCode, resultCode, data);
}

}

我认为此更改会对您有所帮助。

关于java - 为什么我没有收到 onActivityResult 的任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37765477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com