Question

I'm showing up a Dialog on app-start, where you have to select a config. Since it is essential to select one config, I want to "disable" the back-button via a empty onBackPressed().

I got the following code in a DialogFragment:

 public class ChangeConfigDialogFragment extends DialogFragment {

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.dialog_config_change)
                .setItems(R.array.config_array,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // The 'which' argument contains the index
                                // position
                                // of the selected item
                                if (which == 0){
                                    Initiation.BAUDRATE = 500;
                                    Toast.makeText(getActivity(), "Baudrate at " + Initiation.BAUDRATE, Toast.LENGTH_LONG).show();
                                    if (Initiation.getADK() == null){
                                        Initiation.initiateCAN();
                                    }
                                } else if (which == 1) {
                                    Initiation.BAUDRATE = 600;
                                    Toast.makeText(getActivity(), "Baudrate at " + Initiation.BAUDRATE, Toast.LENGTH_LONG).show();
                                    if (Initiation.getADK() == null){
                                        Initiation.initiateCAN();
                                    }
                                } else if (which == 2) {
                                    Initiation.BAUDRATE = 700;
                                    Toast.makeText(getActivity(), "Baudrate at " + Initiation.BAUDRATE, Toast.LENGTH_LONG).show();
                                    if (Initiation.getADK() == null){
                                        Initiation.initiateCAN();
                                    }
                                }

                            }

                        });
        // Create the AlertDialog object and return it
        return builder.create();
    }

    public void onBackPressed(){
        Log.d(getTag(), "are you there?");

    }
}

The problem is, that the onBackPressed() is never been called. Even the log message does not appear.

I tried to clean the project, but no success. Also tried to use a onKeyDown-method from some other topics here on SO. Does anyone has a clue how to solve this?

EDIT:

It works now. .setCancelable(false); worked, but I was to stupid to add it to the Dialog from which the Fragment was called. (instead added it to the builder

Thanks for all your help and time.

Était-ce utile?

La solution

From your question it seems that while your dialog is open you need not to close dialog untill user can select any 1 open from dialog if i am not wrong then,so for this you need not to disable back key but you have to set this two properties for dialog.

dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);

OR

If you want to disable back key then use the below code,

@Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
 if (keyCode == KeyEvent.KEYCODE_BACK) {
 //preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
 return true;
 }
 return super.onKeyDown(keyCode, event);    }

OnbackkeyPressed Requires Api Level 5 Or higher.

@Override
public void onBackPressed() {
}

Autres conseils

I did not try this, but it might work if you set the OnKeyListeneron the builder like this:

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

Try

public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Handle the back button

        if (keyCode == KeyEvent.KEYCODE_BACK) {
                     return false;
        }
        return super.onKeyDown(keyCode, event);
    }

or

dialog.setCancelable(false);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top