Reputation: 2344
I want to display Dialog
for choosing between opening the Camera
/ Gallery
in a Fragment
.
After a Button
is pressed, I show a custom DialogFragment
(this DialogFragment
is an inner
class
inside the Fragment
).
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;
}
}
After one opcion is selected, the Camera
/ Gallery
is initialised, but after making / selecting a picture, the method onActivityResult
is never called.
Here is the code where I create the DialogFragment
:
FragmentManager fm = getActivity().getSupportFragmentManager();
AddPictureDialogFragment addPictureDialogFragment = new AddPictureDialogFragment();
addPictureDialogFragment.show(fm, getTag());
The weird thing is that if I create the DialogFragment
directly in the Fragment
without using the DialogFragment
it works...
I have also tried this, when creating the DialogFragment
:
addPictureDialogFragment.setTargetFragment(this, 1);
and this, when initialising the 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);
}
});
Upvotes: 3
Views: 2537
Reputation: 1843
try this:
private void showAddImageDialog() {
FragmentManager fm = getActivity().getSupportFragmentManager();
AddPictureDialogFragment addPictureDialogFragment = new AddPictureDialogFragment();
addPictureDialogFragment.setTargetFragment(this, REQUEST_CAMERA);
addPictureDialogFragment.show(fm, getTag());
}
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);
getTargetFragment().startActivityForResult(intent, getTargetRequestCode());
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/*");
getTargetFragment().startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
getDialog().dismiss();
}
});
return rootView;
}
}
Upvotes: 4
Reputation: 3520
In your main activity overwrite onActivityResult method as follows.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
if(fragment!=null)
fragment.onActivityResult(requestCode, resultCode, data);
}
}
Sometimes the fragment might be destroyed but the reference might persist and hence the if(fragment!=null) check.
Upvotes: 2
Reputation: 229
This part is very simple
Just call startActivityForResult(intent,constant);
in you fragment class
override the onActivityResult
in activity and don't remove super.onActivityResult(requestCode, resultCode, data);
also override the onActivityResult
in fragment class
In activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "activity result called");
super.onActivityResult(requestCode, resultCode, data);
}
In fragment
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "fragment result called");
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==constant){
Logger.d(TAG, "got result");
}
}
Upvotes: 2
Reputation: 1575
Override onActivityResult in parent Activity i.e. parent of all fragment
Override this method on your Main activity :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
TestFrag demoFragment = (TestFrag) getSupportFragmentManager().findFragmentByTag("test");
demoFragment.onActivityResult(requestCode, resultCode, data);
}
after your fragment activity result call
Upvotes: 1
Reputation: 2825
Please write this code in your Main Actvity.
// In your activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println("onActivityResult Main Activity");
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
Upvotes: 2