Dimitri
Dimitri

Reputation: 118

Android Dialog not dismissing after .dismiss

I have an Dialog that has a recycler view with multiple items or lets say "families", when i click on one of them i want to dismiss that one dialog and open another with another recycler view with the "products" from the family that was clicked and after one of those products from the latest is clicked it should dismiss that one Dialog and open a final one where you edit the product. So it is something like this:

Button in view --> Families Dialog with recycler --Recycler on item click--> Products from that Family with recycler --Recycler on item click--> Edit that item

It works well when i open de Families Dialog, then dismiss it and open the Products one. But when i click on a product from the Products Dialog, the Dialog doesn't get dismissed even tho the Edit Product one opens smoothly, i use the same aproach as when i click an item from the Families one, and for some reason it still doens't work.

I tried:

dialogProducts.dismiss();

dialogProducts.cancel();

dialogProducts.hide();

dialogProducts.dismiss(); on a new Thread

dialogProducts.dismiss(); in a runOnUiThread

....

Here is a sketch of my code:

addButton.setOnClickListener(v -> {


            final Dialog dialogFamilias = new Dialog(getActivity());
             

            //shows families Dialog, lstItemsFamilia is the items in the recycler view
            mostraFamilias(dialogFamilias,lstItemsFamilia);


            RecyclerView recyclerFamilias = dialogFamilias.findViewById(R.id.recyclerAlert);
            

            recyclerFamilias.addOnItemTouchListener(new MyRecycleViewClickListener(getContext(), new MyRecycleViewClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {

                    //this one dismiss works
                    dialogFamilias.dismiss();

                    int idFamiliaClicada = lstItemsFamilia.get(position).getId();


                    final Dialog dialogProducts = new Dialog(getActivity());
                    mostraProdutos(dialogProducts , lstItemsProducts);
                    RecyclerView recyclerProducts = dialogProducts .findViewById(R.id.recyclerAlert);



                    recyclerProducts.addOnItemTouchListener(new MyRecycleViewClickListener(getContext(), new MyRecycleViewClickListener.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            
                            // this one doesn't dismiss
                            dialogProducts.dismiss();


                            int idProdutoClicado = lstItemsProducts.get(position).getId();
                            final Dialog dialogSingleProduct = new Dialog(getActivity());
                            
                            //shows the final dialog to edit the product 
                            mostraEditarProduto(dialogSingleProduct, idProdutoClicado);

                        }


                    }));


                    }

            }));


        });

The MyRecycleViewClickListener looks something like this: (it was taken from How to click recyclerview items in Activity?)

public class MyRecycleViewClickListener implements RecyclerView.OnItemTouchListener {
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    }

    GestureDetector mGestureDetector;

    public MyRecycleViewClickListener(Context context, OnItemClickListener listener) {
        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
        View childView = view.findChildViewUnder(e.getX(), e.getY());
        if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
            mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
}

And the methods that show the dialogs look something like this:

   public void mostraFamilias (Dialog dialogFamilias, List<ItemModel> lstItemsFamilia){


        dialogFamilias.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialogFamilias.setContentView(R.layout.selecionador);
        dialogFamilias.setCanceledOnTouchOutside(true);



        TextView text = (TextView) dialogFamilias.findViewById(R.id.tituloAlert);

        text.setText("Selecionar Família");

        int width = (int)(getResources().getDisplayMetrics().widthPixels*0.90);
        int height = (int)(getResources().getDisplayMetrics().heightPixels*0.90);

        dialogFamilias.getWindow().setLayout(width, height);



        RecyclerView recyclerFamilias = (RecyclerView) dialogFamilias.findViewById(R.id.recyclerAlert);
        RecyclerViewItemsAdapter adapterFamilias = new 
        RecyclerViewItemsAdapter(getContext(),lstItemsFamilia);

        GridLayoutManager manager = new GridLayoutManager(getContext(),3);
        recyclerFamilias.setLayoutManager(manager);

        recyclerFamilias.addItemDecoration(new SpacesItemDecoration(20));
        recyclerFamilias.getRecycledViewPool().setMaxRecycledViews(0, 0);
        recyclerFamilias.setAdapter(adapterFamilias);

        dialogFamilias.show();


    }

The mostraProdutos method is similar.

Upvotes: 0

Views: 348

Answers (1)

Uuu Uuu
Uuu Uuu

Reputation: 1282

Move these line outside

final Dialog dialogFamilias = new Dialog(getActivity());
final Dialog dialogProducts = new Dialog(getActivity());
final Dialog dialogSingleProduct = new Dialog(getActivity());
addButton.setOnClickListener(v -> {
    ...
}

Upvotes: 1

Related Questions