tags:

views:

55

answers:

1

I defined a dialog the following way:

public void displayAvailableDevices(Vector<UserDevice> availableDevices) {

        connector.setDevicesFound(true);
        UserDevice[] devices = new UserDevice[availableDevices.size()];

        for (int i = 0; i < availableDevices.size(); i++) {
            devices[i] = availableDevices.elementAt(i);
        }

        Arrays.sort(devices);

        Vector<UserDevice> sorted = new Vector<UserDevice>();
        for (int j = 0; j < devices.length; j++) {
            sorted.add(devices[j]);
        }

        allAvailableDevices = sorted;

        connector.stopNeighbourDiscovery();
        if (dialog != null) {
            dialog.dismiss();
            checkedUser = null;
        }
        dialog = new Dialog(this);
        dialog.setContentView(R.layout.available_users);
        dialog.setTitle("Available Devices");

        RelativeLayout rootLayer = (RelativeLayout) dialog.findViewById(R.id.dialogRoot);
        rootLayer.setBackgroundColor(Color.WHITE);

        ListView userTable = (ListView) dialog.findViewById(R.id.userList);
        adapter = new UserAdapter();
        userTable.setAdapter(adapter);
        userTable.setOnItemClickListener(new UserListener());

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm); 

        connect = (Button) dialog.findViewById(R.id.userList_connect);
        connect.setMinWidth(dm.widthPixels / 2);
        connect.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (checkedUser != null) {
                    dialog.dismiss();
                    connector.connectToDevice(checkedUser);
                }
            }

        });

        Button start = (Button) dialog.findViewById(R.id.userList_start);
        start.setMinWidth(dm.widthPixels / 2);
        start.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                startProfileView(); 
            }

        });

        Button refresh = (Button) dialog.findViewById(R.id.userList_refresh);
        refresh.setMinWidth(dm.widthPixels / 2);
        refresh.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                getGameBaseLayer().resetAvailableUserDevices();
                connector.refresh();
            }

        });

        Button cancel = (Button) dialog.findViewById(R.id.userList_cancel);
        cancel.setMinWidth(dm.widthPixels / 2);
        cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }

        });

        dialog.show();
        if (checkedUser == null) {
            connect.setEnabled(false);
        }

    }

If this dialog is displayed and the user presses the back button, the dialog is dismissed. However I would like to do some further processing, but how can i detect if the user has pressed the back button?

+1  A: 

I'm quite positive that this article can help a lot. As far as I understand, pressing 'BACK' is like cancelling the dialog (like pressing 'CANCEL'). They show how to make a dialog uncancelable and how to implement a listener to catch the 'cancel' events.

Andreas_D