Show dialog box from another dialog box

I have a DialogFragment that displays a list of options for the user, one of these options is the “Delete” option, when the user clicks the delete option, I want to show the other DialogFragment as confirmation, unfortunately, the confirmation dialog doesn’t show.

here is my code

First piece of code

 public class ContactDialogOption extends SherlockDialogFragment { public static final String TAG = ContactDialogOption.class.getSimpleName(); public ContactDialogOption() { super(); // TODO Auto-generated constructor stub } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setIcon(R.drawable.ic_launcher); builder.setTitle(R.string.options); builder.setItems(new String[] { getString(R.string.call), getString(R.string.send_message), getString(R.string.copy), getString(R.string.edit), getString(R.string.delete) }, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if(which == 4) //delete { FragmentManager mgr = getActivity().getSupportFragmentManager(); FragmentTransaction ft = mgr.beginTransaction(); Fragment old = mgr.findFragmentByTag("SecondFragment"); if (old != null) { ft.remove(old); } ft.addToBackStack(null); fragment.show(ft, fragmentTag); } } }); return builder.create(); } } 
+14
source share
8 answers

I have the same problem, this situation does not occur when you try to open DialogFragment from Fragment .

The only solution I found was to modify the following call:

 fragment.show(ft, fragmentTag); 

To:

 fragment.show(getFragmentManager(), fragmentTag); 

The problem with this solution is that we cannot work with FragmentTransition .

I do not understand why the behavior is different from fragments.

+8
source

I ran into the same problem: not showing another DialogFragment from the positive and negative clicks of the first DialogFragment. My solution was to immediately place the first fragment, which allows you to add and display a second DialogFragment dialog.

// Call this before adding the second dialog fragment activity.getSupportFragmentManager().popBackStackImmediate();

+2
source

Please check the following code. Hope this helps many of you!

 public class SubcategoryFragment extends DialogFragment { public SubcategoryFragment() { } public static SubcategoryFragment newInstance(Integer code, String name) { SubcategoryFragment fragment = new SubcategoryFragment(); mCode = code; mTitle = name; return fragment; } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); liststring = new ArrayList<>(); getAdapter(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_subcategory, container, false); gridView = (GridView) view.findViewById(R.id.sub_grid); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); closeDialog = (ImageView) getDialog().findViewById(R.id.closeDialog); title = (TextView) getDialog().findViewById(R.id.dialogTitle); gridView = (GridView) getDialog().findViewById(R.id.sub_grid); title.setText(String.format("Choose %s", mTitle)); closeDialog.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getDialog().dismiss(); } }); } @Override public Dialog onCreateDialog(@NonNull Bundle savedInstanceState) { Dialog dialog = super.onCreateDialog(savedInstanceState); // request a window without the title dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); // closeDialog = (ImageView) dialog.findViewById(R.id.closeDialog); return dialog; } public void getAdapter() { gridAdapter = new HomeSubGridViewAdapter(getContext(), R.layout.gridview_custom_layout, liststring); gridView.setAdapter(gridAdapter); } } 

This is a method of calling a dialog from a fragment

  fragmentManager = ((FragmentActivity) context).getSupportFragmentManager(); SubcategoryFragment postalFragment = SubcategoryFragment.newInstance(Integer.valueOf(item.getId()), item.getName()); postalFragment.show(fragmentManager, "SubcategoryFragment"); 

Feel free to ask if you feel that the problem is that

+2
source

This is the code that works for me:

 @Override public Dialog onCreateDialog(Bundle savedInstanceState) { if (getArguments() == null) { throw new InvalidParameterException("The key types dialog needs the protocol id to be in the arguments"); } if (mCallback == null) { throw new InvalidParameterException("The key types dialog needs an callback to be set"); } mProtocolId = getArguments().getInt(ApplicationConstants.FragmentsConstants.PROTOCOL_ID); final List<KeyTypeEntity> allKeyTypes = BusinessFacade.getInstance(getActivity()).KeyTypeLogic.getAllKeyTypes(); ArrayAdapter<KeyTypeEntity> keyTypeAdapter = new ArrayAdapter<KeyTypeEntity>(getActivity(), android.R.layout.simple_list_item_1, allKeyTypes); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle("").setAdapter(keyTypeAdapter, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { final KeyTypeEntity keyTypeEntity = allKeyTypes.get(which); AlertDialog.Builder number = new AlertDialog.Builder(getActivity()); List<String> keyNumbers = new ArrayList<String>(); for (int i = 0; i < 50; i++) { keyNumbers.add("" + (i + 1)); } ArrayAdapter<String> kAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, keyNumbers); number.setTitle("").setAdapter(kAdapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { addNewKey(keyTypeEntity, which + 1); } }); number.show(); } }).setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { mCallback.onDialogClosed(); } }); mDialog = builder.create(); return mDialog; } 

In the first click handler, I just create a new dialog and show it. This will close the first dialog, open the second, and when the user clicks on an item in the list, the second click handler is called.

Hope this helps, and I'm not too late :)

0
source

You can pass FragmentManage to newInstance() the First DialogFragment method, after which you can use it to display the new DialogFragment this is my code.

 private static FragmentManager fragmentManager; public static PlayListDialog newInstance(Context context, FragmentManager fragmentManager1) { playListDialog = new PlayListDialog(); mContext = context; fragmentManager = fragmentManager1; return playListDialog; } @Override public void createNewPlaylist() { NewPlayListDialog newPlayListDialog = NewPlayListDialog.newInstance(mContext); newPlayListDialog.showDialog(fragmentManager.beginTransaction(),fragmentManager.findFragmentByTag("newdialog")); } 
0
source

Most recently, I had this problem, and none of the above options worked for me. I tried using the method below:

 DialogFragment fragment = new MyFragment(); //where MyFragment is my fragment I want to show fragment.setCancelable(true); fragment.show(getSupportFragmentManager(), "timePicker"); 

This will ONLY work if you use it in action (to invoke a dialog fragment from an activity class).

However, I fixed this by omitting my instance of activity on AppCompat activity and using it to call getSupportFragment() , as shown below:

 DialogFragment timeFragment = new TimePicker(); timeFragment.setCancelable(true); AppCompatActivity activity = (AppCompatActivity) getActivity(); timeFragment.show(activity.getSupportFragmentManager(), "timePicker"); 

Hope this helps ... Fun coding!

0
source

You can call DialogFragment from another DialogFragment.

 NewDialogFragment newDialogFragment= new NewDialogFragment(); FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction(); newDialogFragment.show(transaction, "New_Dialog_Fragment"); 
0
source

Use this:

getActivity().getSupportFragmentManager

instead

getChildFragmentManager().

Hope this helps.

0
source

All Articles