Close Open Dialog within the RecyclerView Adapter

1

I made a header for my RecyclerView which, when clicked, opens a Dialog. Some time later, it started to give this error:

Activity com.mypkg.myP has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44c46ff0 that was originally added here
05-17 18:24:57.069: ERROR/WindowManager(18850): android.view.WindowLeaked: Activity ccom.mypkg.myP has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44c46ff0 that was originally added here
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.view.ViewRoot.<init>(ViewRoot.java:231)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)

I saw in this post that this would be resolved by closing the Dialog in onPause or onDestroy .

How do I go from within the adapter?

Code that creates Dialog in Adapter :

  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof VHHeader) {
        final VHHeader VHheader = (VHHeader) holder;

VHheader.enviardica.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Dialog dpartilha = new Dialog(ctx);
                dpartilha.requestWindowFeature(Window.FEATURE_NO_TITLE);

                dpartilha.setContentView(R.layout.partilha);

                LinearLayout laydicas = (LinearLayout) dpartilha.findViewById(R.id.laydicas);
                LinearLayout layeventos = (LinearLayout) dpartilha.findViewById(R.id.layeventos);

                dpartilha.show();
            }
        });
    
asked by anonymous 06.12.2016 / 04:51

1 answer

3

Do not create dialogs within other View s (like RecyclerView ). Let Activity take care of displaying this dialog and dismissing it when it is time, while RecyclerView just passes the message asking for the dialog to be created.

The default way to do this without increasing the coupling is to create an interface for Activity implement. An example:

Interface:

public interface ExibidorDeDialogoDePartilha {
    public void exibirDialogoDePartilha();
}

Activity:

public class MinhaActivity extends Activity implements ExibidorDeDialogoDePartilha {

    private AdapterDoMeuRecyclerView adapter;
    private Dialog dialogoDePartilha;

    @Override
    public void exibirDialogoDePartilha() {
       // Insira aqui o código de mostrar o diálogo
    }

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.adapter = new AdapterDoMeuRecyclerView(this);
    }

    @Override
    protected void onDestroy() {
        // Exemplo de código que dispensa o diálogo. Adapte às suas necessidades.
        if (dialogoDePartilha != null && dialogoDePartilha.isShowing()) {
            dialogoDePartilha.dismiss();
        }
    }
}

RecyclerView:

public class AdapterDoMeuRecyclerView extends RecyclerView.Adapter<AdapterDoMeuRecyclerView.ViewHolder> {

    private ExibidorDeDialogoDePartilha exibidorDeDialogoDePartilha;

    public AdapterDoMeuRecyclerView(Context contexto) {
        try {
            this.exibidorDeDialogoDePartilha = (ExibidorDeDialogoDePartilha) contexto;
        } catch (ClassCastException e) {
            throw new ClassCastException("Activity ou Fragment que contém esse RecyclerView deve implementar ExibidorDeDialogoDePartilha.");
        }
    }

// .....

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof VHHeader) {
        final VHHeader VHheader = (VHHeader) holder;

        VHheader.enviardica.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exibidorDeDialogoDePartilha.exibirDialogoDePartilha();
            }
        });
    
06.12.2016 / 06:11