1

Initially in my app I am creating an AlertDialog which has three buttons, in which the middle button opens up another AlertDialog. The problem is that when the second AlertDialog closes after a button is pressed, the first one closes with it. I think both AlertDialogs get closed after I press a button on the second AlertDialog.

What I want is for the first AlertDialog to open another AlertDialog that has its own buttons, and when second AlertDialog presses a button, it only closes itself and goes back to the first one. Is there any way to achieve this?

Here is the code for the button used to open the AlertDialog:

final ImageButton fabgroup = (ImageButton) findViewById(R.id.groupButton);

Here's the code for a button that opens an AlertDialog that contains another button that opens another AlertDialog using the middle button (create button) on itself, but closes them both when a button on the second one is pressed (either the yes or no button, which is not what I want as I only want the second one to close itself and go back to the first AlertDialog, and yea this sounds pretty confusing in theory so I can try to clarify if needed):

    fabgroup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(CreateNote.this);
            helpBuilder.setTitle("Select a group");
            helpBuilder.setMessage("Add to group?");

            final TextView input = new TextView(mainactiv.this);
            input.setSingleLine();
            input.setText("");
            helpBuilder.setView(input);

            helpBuilder.setNegativeButton("Yes",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            // Do nothing but close the dialog
                            Toast.makeText(CreateNote.this, "Page has been added to group", Toast.LENGTH_SHORT).show();
                        }
                    });


            helpBuilder.setNeutralButton("Create", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {


                    //open another alertbox
                    AlertDialog.Builder helpBuilder2 = new AlertDialog.Builder(CreateNote.this);
                    helpBuilder2.setTitle("Assign a new group");
                    helpBuilder2.setMessage("Create group?");

                    final EditText input = new EditText(CreateNote.this);
                    input.setSingleLine();
                    input.setText("");
                    helpBuilder2.setView(input);

                    helpBuilder2.setNegativeButton("Yes",
                            new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int which) {
                                    // Create Group
                                    Toast.makeText(CreateNote.this, "Group has been created", Toast.LENGTH_SHORT).show();
                                }
                            });

                    helpBuilder2.setPositiveButton("No", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Do nothing
                        }
                    });

                    // Remember, create doesn't show the dialog
                    AlertDialog helpDialog2 = helpBuilder2.create();
                    helpDialog2.show();

                }
            });

            helpBuilder.setPositiveButton("No", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // Do nothing
                }
            });

            // Remember, create doesn't show the dialog
            AlertDialog helpDialog = helpBuilder.create();
            helpDialog.show();
        }
    });

Help would be greatly appreciated.

Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
Xians Wu
  • 159
  • 13

2 Answers2

1

I eventually managed to solve this problem by creating two separate functions to generate each dialog box, and when one closes it calls the function to create the other one, kinda like recycling (or maybe closer to looping functions). Although, I'm not entirely sure how performance heavy this is, but it seems to do the job without any issues from what I'm testing. If anyone would like to chime in on how this could be an issue, then I'm open to hearing what others have to say about the negative points of using alert dialog boxes this way.

Xians Wu
  • 159
  • 13
0

You can show an activity as dialog. Put this in your manifest file.

<activity android:theme="@android:style/Theme.Dialog" android:excludeFromRecents="true"/>

From this answer: Android Activity as a dialog

Community
  • 1
  • 1
ikkarion
  • 178
  • 4
  • 17