I have a scenario where I'd like to open a dialogfragment from a button click in an already open dialog fragment. I'd like the original to either stay open and the new one open on top of it or hide the original and then reopen it when the second dialogfragment is dismissed. Just wondering if this is feasible before I go about trying to do it and possibly waste my time. Can anyone advise?
Asked
Active
Viewed 123 times
0
-
For reference if anyone else is trying this, it appears that it is either not possible or more complicated than calling a dialog fragment from an activity. The issue I came across was related to the onAttach method in the dialog fragment. The context at this point was relating to the original calling activity and not the dialogfragment. This would mean that the activity would need to implement the interface instead of the first dialog fragment. As yet I'm not sure if this will break the logic. As I figure this out I'll post a little more which hopefully wil be useful for someone else. – Pale Neanderthal Jun 18 '16 at 12:03
-
This code is where the error is thrown and it is at this point that the initial activity is referenced as needing to implement the interface for the second dialogfragment. `code` @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof OnColourPickerFragmentInteractionListener) { mListener = (OnColourPickerFragmentInteractionListener) context; } else { throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener"); } } `code` – Pale Neanderthal Jun 18 '16 at 12:04
1 Answers
0
Following on from the comments above I have found that the following appears to work (Compiles and runs on a test handset although no logic is yet implemented). I replaced the onAttach method above with the following code.
@Override
public void onAttach(Context context) {
super.onAttach(context);
TextPropertiesDialogFragment prev = (TextPropertiesDialogFragment) getFragmentManager().findFragmentByTag("TextPropertiesDialogFragment");
if (prev != null) {
if (prev instanceof OnColourPickerFragmentInteractionListener) {
mListener = (OnColourPickerFragmentInteractionListener) prev;
} else {
throw new RuntimeException(prev.toString()
+ " must implement OnFragmentInteractionListener");
}
}
}
For reference this onAttach method is in the second dialog fragment called OnColourPickerDialogFragment. The TextPropertiesDialogFragment is the first dialogfragment that is called from the activity. For completeness I've included the definition of the listener and interface below.
public class TextPropertiesDialogFragment extends DialogFragment implements View.OnClickListener, ColourPickerDialogFragment.OnColourPickerFragmentInteractionListener{
public void colourPickerCallBackMethod(Bundle bundle){
//Do some work here
}
//........
}
public class ColourPickerDialogFragment extends DialogFragment implements View.OnClickListener {
private View topLevelFragmentView;
private OnColourPickerFragmentInteractionListener mListener;
private Bundle callBackBundle;
public interface OnColourPickerFragmentInteractionListener{
void colourPickerCallBackMethod(Bundle callBackBundle);
}
//......
}

Pale Neanderthal
- 61
- 5