使用后退按钮返回第一个对话框窗口

时间:2012-01-29 18:43:33

标签: android android-activity dialog alertdialog

当新的或更确切地说下一个对话框正在运行时,我无法返回第一个对话框窗口。

我的目标是通过单击后退按钮打开对话框。我怎么能实现这个?

我尝试了几件事,但无法解决我的问题。这是我的代码:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("AP's");
                builder.setItems(scannedAP, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            Cursor cursor = dbAdapter.queryRssiBlob(scannedMacSsid[which][mac],scannedMacSsid[which][ssid],coord[GridConfig.curXCoord],coord[GridConfig.curYCoord]);
                            if (cursor != null && cursor.moveToFirst()) {
                                byte[] rssiArrayOne = cursor.getBlob(cursor.getColumnIndex("rssi"));
                                cursor.close();
                                ObjectInputStream objectIn = new ObjectInputStream(new ByteArrayInputStream(rssiArrayOne));
                                List<Integer> readRssi = (List<Integer>) objectIn.readObject();
                                rssiList = new CharSequence[readRssi.size()];
                                for (int index = 0; index < readRssi.size(); index++) {
                                    rssiList[index] = "" + readRssi.get(index);
                                }
                                AlertDialog.Builder build = new AlertDialog.Builder(context);
                                build.setTitle("RSSi values");
                                build.setItems(rssiList, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {}
                                    });
                                build.setOnKeyListener(new DialogInterface.OnKeyListener() {
                                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event2) {
                                        if (keyCode == KeyEvent.KEYCODE_BACK) {
                                            return false;
                                        }
                                        return true;
                                    }   
                                    });
                                AlertDialog alertBuild = build.create();
                                alertBuild.show();
                            } else {
                                Toast.makeText(context, "No Values", Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }

                });
                AlertDialog alert = builder.create();
                alert.show();

1 个答案:

答案 0 :(得分:0)

为第一个对话框使用单独的类,并将android:theme="@android:style/Theme.Dialog"添加到AndroidManifest中该类的<activity>标记。如果您不致电finish(),它会被添加到后台。