Everytime the `AlertDialog` is showing and the application is closed or destroyed I get this error in the logcat `has leaked window`. I know that using `onDestroy()` method below removes that error but that only works if the `AlertDialog` is inside the activity. However in my case I'm using the `AlertDialog` inside the adapter which is shown on the code below

```
@Override
public void onDestroy(){
    super.onDestroy();
    if(alertDialog != null && alertDialog.isShowing()) {
        alertDialog.cancel();
    }
}

```

Logcat Error.

```
Activity com.test.example.MovieList has leaked window 
com.android.internal.policy.impl.PhoneWindow$DecorView{1ed0b473 V.E..... R....... 
    0,0-1080,543} that was originally added here at android.view.ViewRootImpl.<init>
        (ViewRootImpl.java:363)

```

Adapter

```
    public class MyAdapter extends RecyclerView.Adapter <MyAdapter.ViewHolder>{

        private List<Movies> moviesList;
        private Context mContext;
        AlertDialog alertDialog;

    public MyAdapter (Context context, List<Movies> list) {
            mContext = context;
            moviesList= list;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            mContext = parent.getContext();
            View view = View.inflate(parent.getContext(), R.layout.movies, null);
            return new ViewHolder(view);
        }

    @Override
        public void onBindViewHolder(final ViewHolder holder, int viewType) {
        final Movies movies = moviesList.get(holder.getAdapterPosition());
        final String id = movies.getId()

        holder.title.setText(movies.getTitle());

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
             AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.AppCompatAlertDialogStyle);
                builder.setTitle(R.string.dialog_title);

                builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                         moviesList.remove(holder.getAdapterPosition());
                         DBHelper db = new DBHelper (mContext);
                         db.deleteMovie(movie);
                         notifyDataSetChanged();
                         db.close();
                         dialog.dismiss();
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                  }
                });

                alertDialog = builder.create();
                alertDialog.show();
                return false;
            }
        });
    }

       @Override
        public int getItemCount() {
            return (moviesList != null? moviesList.size():0);
        }

        class ViewHolder extends RecyclerView.ViewHolder {

            TextView title;

            ViewHolder(View itemView) {
                super(itemView);
                title = (TextView) itemView.findViewById(R.id.movie_title);
            }

        }
    }

```