Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android DialogFragment doesn't dismiss

I have a custom dialog in Android, i create the dialog view on the onCreateView method.

public class FiltroDialog extends DialogFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

When i try to close the dialog, i have a method inside the dialog called by an onClickListener

boton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
                            Log.d(TAG,"doDismiss");
            FiltroDialog.this.dismiss();
        }
    });

I have overrided several onMethods of the dialog to see whats happening with the dialog.

@Override
public void onDestroyView() {
    super.onDestroyView();
    Log.d(TAG, "onDestroyView");
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    Log.d(TAG, "onAttach");
}

@Override
public void onDetach() {
    super.onDetach();
    Log.d(TAG, "onDetach");
}

@Override
public void onStart() {
    super.onStart();
    Log.d(TAG, "onStart");
}

@Override
public void onStop() {
    super.onStop();
    Log.d(TAG, "onStop");
}

    @Override
public void onCancel(DialogInterface dialog) {
    super.onCancel(dialog);
    Log.d(TAG,"onCancel");
}

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    Log.d(TAG,"onDismiss");
}

I tried to close the dialog with dismiss(); dismissAllowingStateLoss(); with a FragmentTransaction and everytime i have the same result.

D/FiltroDialog(30492): doDismiss
D/FiltroDialog(30492): onStop
D/FiltroDialog(30492): onDestroyView
D/FiltroDialog(30492): onDetach

But the dialog doesn´t dissapear from the screen, with the back button, the dialog dissapears correctly but with the dismiss() or FragmentTransaction.remove method i only get to Stop, DestroyView, Detach but not onDismiss()

When i click the button again, only this is shown in LogCat

D/FiltroDialog(30492): doDismiss

I tried several ways to dismiss the dialog. Here is the code i use to show the dialog from the activity (tried too from a fragment)

FiltroDialog newFragment = FiltroDialog.newInstance();
newFragment.show(getSupportFragmentManager(), TAG_DLG_FILTROS); 

Here is the code to try to close the dialog from the activity (tried too from a fragment, same result)

DialogFragment prev = (DialogFragment) getSupportFragmentManager().findFragmentByTag(TAG_DLG_FILTROS);
if (prev != null) {
    prev.dismiss();         
}

Here is another try to try to close from the activity (tried too from a fragment, same result)

Fragment prev = getSupportFragmentManager().findFragmentByTag(TAG_DLG_FILTROS);
        if (prev != null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.remove(prev);
            ft.commit();
    }

Here is another try to close from the dialog trying to call the cancel() method of the dialog inside the DialogFragment.

boton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {               
            Log.d(TAG,"doDismiss");
            FiltroDialog.this.getDialog().cancel();
        }
    });

Doesn't dismiss the dialog and doesn't call the onDismiss neither Stop, DestroyView, Detach methods.

Here is another try to close from the dialog trying to call the dismiss() method of the dialog inside the DialogFragment.

boton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {               
            Log.d(TAG,"doDismiss");
            FiltroDialog.this.getDialog().dismiss();
        }
    });

Doesn't dismiss the dialog and doesn't call the onDismiss neither Stop, DestroyView, Detach methods.

I have some dialogs in the app, and never had that problem, i am not totally a newbie in android programming but i don't know what i am doing wrong? It's a bug in the compat library?

like image 280
Angel Ivorra Avatar asked Jul 03 '13 12:07

Angel Ivorra


1 Answers

I ran into the same problem today.

How are you handling the views in the dialog? For me the problem was that I had a FrameLayout in which I then inflated the view into (based on some information). I cant tell why this broke it but it did. When I replaced the framelayout with a simple viewstub it worked perfectly fine.

Hope it works out. Good luck!

like image 155
zoltish Avatar answered Oct 16 '22 03:10

zoltish