Previous AlertDialog hides when trying to open one more AlertDialog

manishjoshimanishjoshi USMember
edited September 2016 in Xamarin.Android

I am trying to pop up AleartDialog from another dialog and it happenes successfully but the problem here is :

when second dialog opens up on click of 'Add Hostess' button in first pop up dialog the latter one goes invisible and when I go back it pops up automatically.

Attached images would make the requirement more clear

Below is the code for creating AlertDialog

public class DialogBuilder : DialogFragment {

    public static DialogBuilder NewInstance(bool showPrimary) {
        DialogBuilder fragment = new DialogBuilder { Arguments = new Bundle() };
        fragment.Arguments.PutBoolean("boolean", showPrimary);
        return fragment;
    }
    public override void OnCreate(Bundle savedInstanceState) {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Use this to return your custom view for this Fragment
       // bool showPrimaryLayout = Arguments.GetBoolean("boolean");
        View view = null;
        bool showPrimaryLayout = Arguments.GetBoolean("boolean");
        InputMethodManager inputManager = (InputMethodManager)Activity.GetSystemService(Context.InputMethodService);
        var currentFocus = Activity.CurrentFocus;
        if (currentFocus != null) {
            inputManager.HideSoftInputFromWindow(currentFocus.WindowToken, HideSoftInputFlags.none);
        }
        if (showPrimaryLayout) {
            view = inflater.Inflate(Resource.Layout.dialog_edit_schedule_party, container, false);

            var button_add_hostess = view.FindViewById<Button>(Resource.Id.btn_add_hostess);
            button_add_hostess.Click += (s, e) => ShowDialog(false);
        }
        else {
            view = inflater.Inflate(Resource.Layout.fragment_add_hostess_dialog, container, false);
        }

        return view;
    }
    public void ShowDialog(bool isPrimary) {

        FragmentTransaction transction = FragmentManager.BeginTransaction();
        //Remove fragment else it will crash as it is already added to backstack
        Fragment prev = FragmentManager.FindFragmentByTag("dialog");
        if (prev != null) {
            transction.Remove(prev);
        }

        transction.AddToBackStack(null);
        DialogBuilder builder = NewInstance(isPrimary);
        builder.Show(transction, "dialog");
    }

}

}

below is how I called in my Fragment class

         private void popUpDialog() {
            FragmentTransaction ft = FragmentManager.BeginTransaction();
            //Remove fragment else it will crash as it is already added to backstack
            Fragment prev = FragmentManager.FindFragmentByTag("dialog");
            if (prev != null) {
                ft.Remove(prev);
            }

            ft.AddToBackStack(null);

            // Create and show the dialog.
            DialogBuilder dialog = DialogBuilder.NewInstance(true);
            dialog.Cancelable = false;

            //Add fragment
            dialog.Show(ft, "dialog");
        }
Sign In or Register to comment.