0

I'm kinda puzzled here while creating a custom Progress DialogFragment. Everything works fine but since I don't want the user to "go back" until the DialogFragment is dismissed, I'm trying to capture the KeyEvent and "disable".

While this works great:

    @Override
public ProgressDialog onCreateDialog(Bundle savedInstanceState) {

    final ProgressDialog dialog = new ProgressDialog(getActivity());
    dialog.setMessage(getString(R.string.loading_text));
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);

    // Disable the back button
    OnKeyListener keyListener = new OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface dialog, int keyCode,
                KeyEvent event) {
            if( keyCode == KeyEvent.KEYCODE_BACK){  
                return true;
            }
            return false;
        }
    };
    dialog.setOnKeyListener(keyListener);
    return dialog;
}

Using onCreateDialog does not let me inflate the Fragment correctly and thus, customize the look&feel of my Fragment. onCreateDialog, on the other hand, captures back key pressed event perfectly. When switching to onCreateView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    //Inflate the XML view for the help dialog fragment
    View view = inflater.inflate(R.layout.progress_dialog_fragment, container);
    TextView text = (TextView)view.findViewById(R.id.loadingMessage);
    text.setText(Html.fromHtml(getString(R.string.loading_text)));

    // Disable the back button
    android.view.View.OnKeyListener keyListener = new android.view.View.OnKeyListener() {

        @Override
        public boolean onKey(View view, int keyCode, KeyEvent event) {
            if( keyCode == KeyEvent.KEYCODE_BACK){  
                return true;
            }
                return false;
        }
    };
    view.setOnKeyListener(keyListener);

    // request a window without the title
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    //Transparent Dialog background
    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));
    return view;
}

The event is not getting captured. This leads me to another question...since I've seen using onCreateDialog and onCreateView in distinctively in many SO questions...what is the difference between the two really?

Thanks!

AlejandroVK
  • 7,373
  • 13
  • 54
  • 77

0 Answers0