12

After setting:

MyDialogFragment fragment = new MyDialogFragment();
fragment .setCancelable(false);

it is still dismissed after click on search button. and i haven't found option like in activity to override onKeyDown();

I need create dialog that will be shown until my "dismiss" button is pressed.Please Help

aleb
  • 2,490
  • 1
  • 28
  • 46
Hoochwo
  • 121
  • 1
  • 4
  • I strongly encourage you to redesign your UI such that this is not needed. http://blog.radioactiveyak.com/2010/08/what-you-can-do-with-your-modal-dialogs.html – CommonsWare Jun 01 '11 at 12:10
  • Put the message in whatever fragment that triggered the need for the message. Most Web apps, for example, display error messages inline on the page, not via a modal dialog. – CommonsWare Jun 01 '11 at 14:33

2 Answers2

8

I've just found an answer. After DialogFragment was created we can get the its dialog

Dialog dialog = getDialog();

if( null!= dialog)
{
   dialog.setOnKeyListener(new OnKeyListener()
{

        @Override
    public boolean onKey ( DialogInterface dialog , int keyCode , KeyEvent event )
    {
        // disable search button action
        if (keyCode == KeyEvent.KEYCODE_SEARCH)
        {
            return true;
        }
        return false;
    }
});
}
Marcus
  • 12,296
  • 5
  • 48
  • 66
Hoochwo
  • 382
  • 4
  • 14
  • DialogFragment is NOT a Dialog and it does NOT have setOnKeyListener. – User Jan 11 '14 at 21:44
  • 5
    @Ixx But it contains the dialog so the answer I provided solve the problem. Your comment is completely irrelevant. – Hoochwo Jan 13 '14 at 13:46
  • FYI, this is the correct answer. Thanks to Hoochwo for this technique. Google should have made onCancel return a true/false to see whether it should be cancelled/dismissed instead of just having a method that lets you know the dialogfragment was cancelled via the back button :-P – kenyee Jan 04 '17 at 21:00
0

getFragmentManager().popBackStack();

Hope this helps!

Nathan Schwermann
  • 31,285
  • 16
  • 80
  • 91
  • @Hoochwo until button isn't pressed? meaning they need to hold the button down to view the dialog? – Nathan Schwermann Jun 02 '11 at 13:28
  • @Hoochwo looks like a bug I would think what you are doing should work. As a temporary fix I would suggest overriding onKeyDown in the container activity to stop the back button from working if you do a findFragmentById and find its an instanceof your dialogframent. – Nathan Schwermann Jun 05 '11 at 16:43