Reputation: 1934
I found a custom BlurDialog on github for android. It opens up a new dialog window with options and blurs the background of the rest of the screen. I have it working perfectly, but I am running into an issue where if I select an item in the options list, it will launch the correct activity, but then when I hit the back button I get back to the previous activity and the dialog window is still open. The only way to close it is by clicking outside the dialog window.
I am trying to find out how to close the dialog window before launching the new activity, so that when the user goes back to that screen the dialog window isn't open anymore. I am still new to android, so I am sure I am missing something extremely simple, I just can't figure it out. Here is the "onCreateDialog" method in my fragment that creates the blurred dialog window...
@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 CLOSE 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 have tried looking through the sample apps on the github page here Blurred Dialog Github
But it only shows how to activate the dialog, not how to actually close it when a user selects an item in the dialog. Everything else is working perfectly, if I click an item it launches the correct activity, and if I click outside the dialog the dialog closes. I just need to close it programmatically after a user clicks an item in the dialog. Thank you.
Upvotes: 1
Views: 65
Reputation: 4132
Instead of final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
create dialog using final AlertDialog builder = new AlertDialog.Builder(getActivity()).create()
and show the dilaog after stting view like dialog.show()
.
Whenver user clicks something on the screen you can close the dialog using the dialog object like dialog.dismiss()
i.e inside your follow button click
followButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss()
//YOUR CODE SHOULD COME HERE
}
});
Upvotes: 1
Reputation: 76
According to the developer site https://developer.android.com/reference/android/app/DialogFragment.html
"Note that in this case the fragment is not placed on the back stack, it is just added as an indefinitely running fragment. Because dialogs normally are modal, this will still operate as a back stack, since the dialog will capture user input until it is dismissed. When it is dismissed, DialogFragment will take care of removing itself from its fragment manager."
Maybe if you call dismiss() it will work.
Public methods void dismiss() Dismiss the fragment and its dialog.
Upvotes: 0