Skip to content

Programmatically close dialog on item click #90

@danwguy

Description

@danwguy

I am using the sample to create a new blur dialog and everything is working great, when I select an item from the list I start a new activity, and it all works great, but if I hit the back button in the action bar when I get back to the activity that has the blurred dialog the dialog is still open. I have to click somewhere on the screen that is not in the dialog to make it go away. I can't find a way to programmatically close the dialog when a user selects an item in the dialog list.

I am using the following method to create the dialog in a fragment...

@Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_profile_dialog, null);
        TextView text = (TextView) view.findViewById(R.id.follow_or_unfollow);
        if(isSelf) {
            text.setText("Edit Profile");
        } else {
           text.setText((isFollowing) ? "UnFollow" : "Follow");
        }
        RelativeLayout followButton = (RelativeLayout) view.findViewById(R.id.follow_layout);
        RelativeLayout chatButton = (RelativeLayout) view.findViewById(R.id.chat_layout);


        followButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //NEED TO DISMISS DIALOG WINDOW HERE
                if(isSelf) {
                    //activate ProfileEditActivity
                    getActivity().startActivity(new Intent(getActivity(), EditProfileActivity.class));
                } else {
                    Map<String, Object> updates = new HashMap<>();
                    if(isFollowing) {
                        //unfollow
                        updates.put("/following/" + user.getUid() + "/users/" + userID, null);
                        updates.put("/followers/" + userID + "/users/" + user.getUid(), null);
                    } else {
                        updates.put("/following/" + user.getUid() + "/users/" + userID, true);
                        updates.put("/followers/" + userID + "/users/" + user.getUid(), true);
                        //follow
                    }
                    database.getReference().updateChildren(updates);
                    getFragmentManager().beginTransaction().remove(ProfileDialogFragment.this).commit();
                }
            }
        });

        chatButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(), "Implement the start activity for chat", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setView(view);
        return builder.create();
    }

I don't see any example of how to close the dialog when a user selects an item from the dialog window. Please help, I am still pretty new to android and have no idea how to get this dialog to close once the user selects an item in the dialog. Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions