0

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.

Fraggles
  • 463
  • 4
  • 20
  • 1
    `onBackPressed()` is an activity and not fragment method - you're not overriding anything. What are you trying to achieve by overriding it? – laalto Feb 11 '14 at 09:30
  • How about setting a KeyListener ? – Triode Feb 11 '14 at 09:35
  • I tried it, because in another app it worked. So I just copied the code of `onBackPressed()` and thought it would work, as it didn't I started to search for answers – Fraggles Feb 11 '14 at 09:38
  • Instead of serching for answer can't you check what the developer doc say ? – Triode Feb 11 '14 at 09:40
  • As I understand, DialogFragment can't handle `onBackPressed()` cause its an Activity method. So I have to handle the `onBackPressed()` of my Activity. In the Developer docs I could not find it, so is there something to check from inside my `Activity` if the Dialog is "alive" ? – Fraggles Feb 11 '14 at 09:57

4 Answers4

3

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() {
}
i.n.e.f
  • 1,773
  • 13
  • 22
  • `setCancelable(false);` does not disable the BackButton functionality. KeyDown does not work, because its not defined for DialogFragment and onBackPressed also cause its an Activity method – Fraggles Feb 11 '14 at 09:54
  • you just need to set this property for your dialog box dialog.setCanceledOnTouchOutside(false); & for disabling back button check this link http://stackoverflow.com/questions/4779954/disable-back-button-in-android – i.n.e.f Feb 11 '14 at 09:55
  • I can't create a AlertDialog object, because the constructor is not visible :( – Fraggles Feb 11 '14 at 10:05
  • you were right @i.n.e.f, I was just to stupid to attach it to dialog :( – Fraggles Feb 11 '14 at 10:26
1

Use builder.setCancelable(false)

http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setCancelable(boolean)

Pararth
  • 8,114
  • 4
  • 34
  • 51
1

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);
Prasanth S
  • 3,725
  • 9
  • 43
  • 75
1

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;
        }
    }
});
super-qua
  • 3,148
  • 1
  • 23
  • 30