我想要顯示Dialog
以便在Fragment
之間打開Camera
/Gallery
。一個Button
按下Android onActivityResult永遠不會從片段中的DialogFragment調用
後,我顯示自定義DialogFragment
(這DialogFragment
是inner
class
的Fragment
內)。被選擇的一個opcion
public static class AddPictureDialogFragment extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_add_picture_dialog, container, false);
getDialog().setTitle("New Picture");
Button openCameraButton = (Button) rootView.findViewById(R.id.open_camera_button);
openCameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("","Open Camera Option Selected");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
getDialog().dismiss();
}
});
Button openGalleryButton = (Button) rootView.findViewById(R.id.open_gallery_button);
openGalleryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("","Open Gallery Option Selected");
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);
getDialog().dismiss();
}
});
return rootView;
}
}
後,Camera
/Gallery
被初始化,但撥打/選擇畫面之後,該方法onActivityResult
不會被調用。
下面是代碼,我創建DialogFragment
:
FragmentManager fm = getActivity().getSupportFragmentManager();
AddPictureDialogFragment addPictureDialogFragment = new AddPictureDialogFragment();
addPictureDialogFragment.show(fm, getTag());
奇怪的是,如果我直接在Fragment
創建DialogFragment
不使用它的工作DialogFragment
...
我有當創建DialogFragment
時也嘗試過:
addPictureDialogFragment.setTargetFragment(this, 1);
and thi S,初始化的camera
Intent
時:
Button openCameraButton = (Button) rootView.findViewById(R.id.open_camera_button);
openCameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("","Open Camera Option Selected");
getDialog().dismiss();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
getTargetFragment().onActivityResult(getTargetRequestCode(), 1, intent);
startActivityForResult(intent, REQUEST_CAMERA);
}
});
那個作品!謝謝! – Ale