Home > OS >  Android - Dismiss/Hide does not dismiss the dialog
Android - Dismiss/Hide does not dismiss the dialog

Time:11-03

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

CodePudding user response:

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 of the customizations of the dialog 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);

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

CodePudding user response:

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

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

now when you want to dismiss it use

recipeLoading.dismiss();

it will dismiss the dialog which is being shown

  • Related