I’m Using Dialogname.Show() method to show a loading dialog this is working fine however when I try to dismiss it it does not work I have used Dialogname.hide(), Dialogname.cancel(), Dialogname.dismiss()
Dialog is an acitivity
public class RecipeLoading extends Dialog {
public RecipeLoading(@NonNull Context context){
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.meetvishalkumar.myapplication.R.layout.activity_recipe_loading);
}
}
Dialog Dismiss Code
`
RecipeLoading recipeLoading = new RecipeLoading(RecipeDetailsActivity.this);
recipeLoading.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
recipeLoading.hide();
recipeLoading.cancel();
recipeLoading.dismiss();
`
Dialog Show Code
`
RecipeLoading recipeLoading = new RecipeLoading(RecipeDetailsActivity.this);
recipeLoading.setCancelable(false);
recipeLoading.getWindow().setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
recipeLoading.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
recipeLoading.show();
`
i’m Using Dialogname.Show() method to show a loading dialog this is working fine however when i try to dismiss it it does not work i have used Dialogname.hide(), Dialogname.cancel(), Dialogname.dismiss()
2
Answers
The issue is First you create an object of dialog and show it (Good)
Now when you try to dismiss it you create a new object of dialog again and then try to hide it instead of calling the hide or dismiss or cancel method of the old object of dialog, try like this for showing
now when you want to dismiss it use
it will dismiss the dialog which is being shown
when your calling
RecipeLoading recipeLoading = new RecipeLoading(RecipeDetailsActivity.this);
in the second time, your living the previousrecipeLoading
and calling a new one and when you calling therecipeLoading.dismiss();
you basically calling to dismiss the secondrecipeLoading
that you created (who haven’t shown yet)so i suggest you to declare at the top of your class
and create it whenever you want to show it and then when you want to dismiss it just call
recipeLoading.dismiss();
your code should look like this:
when you want to show the dialog just call
and to dismiss it call
to keep things more organized do all the dialog customizations inside the dialog class, like this: