Android: Cómo evitar que el botón atrás cancele un DialogFragment


Tengo un Fragmento que puede crear y abrir un DialogFragment, pero cuando presiono el botón atrás, descarta el diálogo aunque llame explícitamente a setCancelable(false); ¿Hay alguna manera de que mi DialogFragment sea insensativo al botón atrás?

public class LoadingDialogFragment extends DialogFragment
{
    String title;
    String msg;

    public LoadingDialogFragment()
    {
        this.title = "Loading...";
        this.msg = "Please wait...";
    }
    public LoadingDialogFragment(String title, String msg)
    {
        this.title = title;
        this.msg = msg;
    }

    @Override
    public Dialog onCreateDialog(final Bundle savedInstanceState)
    {
        final ProgressDialog dialog = new ProgressDialog(getActivity());

        dialog.setTitle(title);
        dialog.setMessage(msg);
        dialog.setIndeterminate(true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);

        return dialog;
    }

}

Creo el DialogFragment a partir de una AsyncTask:

private class GpsTask extends AsyncTask<String, Integer, Integer>
{
    //ProgressDialog dialog;
    @Override
    protected void onPreExecute()
    {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        DialogFragment newFragment = new LoadingDialogFragment("Gathering Location", "Acquiring GPS lock...");
        ft.addToBackStack(null);
        newFragment.show(ft, "dialog");
    }

    @Override
    protected Integer doInBackground(String... params)
    {
        //acquire a GPS lock and grab a few position updates
    }

    @Override
    protected void onProgressUpdate(Integer... input) { }

    @Override
    protected void onPostExecute(Integer result)
    {
        getSupportFragmentManager().popBackStackImmediate();
    }
}
Author: Adil Hussain, 2012-04-16

3 answers

Cómo usar setCancelable? ¿Lo probaste?

De los Documentos -

Controla si el diálogo mostrado es cancelable. Usa esto en lugar de llamando directamente al diálogo.setCancelable (booleano), porque DialogFragment necesita cambiar su comportamiento basado en esto

 106
Author: Lalit Poptani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-16 09:47:43

No estoy del todo seguro de si esto funcionará con FragmentDialogs, pero si el setCancelable no funcionó para usted, podría valer la pena echar un vistazo a este artículo: Android: Solicitar al usuario que guarde los cambios cuando se presiona el botón Atrás

Explica cómo detectar el botón atrás que se está presionando. Así que tal vez usted puede suprimir la prensa del botón y va a detener el diálogo de cierre?

 4
Author: manavo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 11:54:25

Puede ayudarte.

newFragment.setCancelable(false); 

Realice cambios como los anteriores al crear el objeto DialogFragment o en el constructor de DialogFragment Personalizado como en el ejemplo siguiente.

public static CustomClearHourDialog newInstance(Bundle args, IDialogListener listener)
        {
            CustomClearHourDialog clearHourDialog = new CustomClearHourDialog();            
            CustomClearHourDialog.listener = listener;
            clearHourDialog.setCancelable(false);
            return clearHourDialog;
        }
 3
Author: Karthikeyan Ve,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-13 11:38:20