Android – Dismiss/Hide does not dismiss the dialog

Issue

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()

Solution

when your calling RecipeLoading recipeLoading = new RecipeLoading(RecipeDetailsActivity.this); in the second time, your living the previous recipeLoading and calling a new one and when you calling the recipeLoading.dismiss(); you basically calling to dismiss the second recipeLoading 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:

public class RecipeDetailsActivity extends Activity {
    
    // diclaring a RecipeLoading named "recipeLoading"
    RecipeLoading recipeLoading;

    // onCreate() etc.
}

when you want to show the dialog just call

recipeLoading = new RecipeLoading(this);
recipeLoading.show();

and to dismiss it call

recipeLoading.dismiss();

to keep things more organized do all the dialog customizations inside the dialog class, like this:

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);

        setCancelable(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
    }
}

Answered By – J El

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published