Saturday 14 February 2015

How to dismiss the DialogFragment in Android ?

* There are two ways you can dismiss a dialog fragment.

* The first is to explicitly call the dismiss() method on the dialog fragment in response to a button or some action on the dialog view.

1- Calling dismiss() :

if ( someview.getId() == R.id.btn_dismiss)
{
    //use some callbacks to advise clients
    //of this dialog that it is being dismissed
    //and call dismiss
     dismiss();
     return;
}

* The other way to dismiss a dismiss a dialog fragment is to present another dialog fragment.
The way you dismiss the current dialog and present the new one is slightly different than just dismissing the current dialog.

2- Setting up a dialog for a back stack

 if ( someview.getId() == R.id.btn_invoke_another_dialog )
{
   Activity act = getActivity();
   FragmentManager fm = act.getFragmentManager();
   FragmentTransaction ft = fm.beginTransaction();
   ft.remove(this);

   ft.addToBackStack(null);
   //null represents no name for the back stack transaction

   HelpDialogFragment hdf = HelpDialogFragment.newInstance(R.string.helptext) ;
   hdf.show( ft, "HELP");
   return;
}


No comments:

Post a Comment