Android DialogFragment doesn't dismiss
Asked Answered
N

6

6

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?

Neruda answered 3/7, 2013 at 12:25 Comment(6)
are you sure boton.setOnClickListener is invoked?Gibbsite
Yes, in the onClickListener i put a Log.d() call to ensureNeruda
and if you try to dismiss it more times?Gibbsite
After the dismiss call, if i click the button again gets a nullPointerException because the getView() call inside the dialog returns null. The second click doesn't have any effects.Neruda
and if you call cancell instead of dismiss ? Does it make any difference?Gibbsite
The DialogFragment doesn't have a cancel() mehotd, i have to use getDialog().cancel() or getDialog().dismiss(), i tried both, and updated the question, doesn't call the onStop, onDismiss(), nothing.Neruda
U
4

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!

Unmake answered 12/8, 2013 at 11:28 Comment(3)
i am in the same situation.. what do you mean by "viewstub"?Connotative
Using (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) instead of android.support.v4.app.DialogFragment.getLayoutInflater(Bundle) just seems to work fine. cool! in the javadoc of the latter there is also a "@hide" in the comments so I believe it's just a function not to be used.Connotative
Weird, the LayoutInflater indeed was causing this!Abettor
S
0

Hmm, can't see why that isn't working but you could always do getActivity().onBackPressed() as an interim workaround

Sitting answered 3/7, 2013 at 15:42 Comment(1)
I know that, but what i am looking for is a solution or explanation, i am changing the dialog to a Fragment to acomplish the same result, but i am developing a large app in android and these things scares me a lot. Thanks for the response anyway.Neruda
C
0

Starting from @zoltish answer i discovered that inflating a view into the dialog contents after having created the actual view creates this kind of troubles. In onCreate I was inflating a layout, and then using a post to invoke an onViewCreated facility where I was inflating another layout inside the first one. The solution is that all the layout/view hierarchy must have been loaded from onCreate, this on 4.3 emulator at least.

Connotative answered 11/11, 2013 at 16:25 Comment(0)
N
0

To turn Gianluca P.'s comment into an answer:

It seems that using the wrong LayoutInflater can break the dismiss functionality. If you get a LayoutInflater from android.support.v4.app.DialogFragment.getLayoutInflater(Bundle) and then use it to inflate views, dismiss will be broken.

To fix the problem, use (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) instead.

Just one of those Android things.

Nineteenth answered 5/6, 2015 at 14:56 Comment(0)
F
0

I was having the same problem and I moved the DialogFragment's reference to a method's local scope which worked for me. (Initially, it was my class's field).

Feud answered 20/11, 2017 at 7:21 Comment(0)
H
0

Why don't you try using only this code:

dismiss();

If you want to dismiss the Dialog Fragment by its own. You can simply put this code inside the dialog fragment where you want to dismiss the Dialog.

For example:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        dismiss();
    }
});

This will close the recent Dialog Fragment that is shown on the screen.

Hope it helps for you.

Hound answered 14/3, 2019 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.