Merge "Fixed multiple Bluetooth Settings crashes." into lmp-dev
diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java
index 1282409..826a451 100755
--- a/src/com/android/settings/bluetooth/BluetoothSettings.java
+++ b/src/com/android/settings/bluetooth/BluetoothSettings.java
@@ -392,7 +392,7 @@
 
             final CachedBluetoothDevice device = (CachedBluetoothDevice) v.getTag();
             final Activity activity = getActivity();
-            DeviceProfilesSettings profileFrag = (DeviceProfilesSettings)activity.
+            DeviceProfilesSettings profileFragment = (DeviceProfilesSettings)activity.
                 getFragmentManager().findFragmentById(R.id.bluetooth_fragment_settings);
 
             if (mSettingsDialogView != null){
@@ -402,23 +402,26 @@
                 }
             }
 
-            if (profileFrag == null) {
+            if (profileFragment == null) {
                 LayoutInflater inflater = getActivity().getLayoutInflater();
                 mSettingsDialogView = inflater.inflate(R.layout.bluetooth_device_settings, null);
-                profileFrag = (DeviceProfilesSettings)activity.getFragmentManager()
+                profileFragment = (DeviceProfilesSettings)activity.getFragmentManager()
                     .findFragmentById(R.id.bluetooth_fragment_settings);
 
                 // To enable scrolling we store the name field in a seperate header and add to
-                // the ListView of the profileFrag.
+                // the ListView of the profileFragment.
                 View header = inflater.inflate(R.layout.bluetooth_device_settings_header, null);
-                profileFrag.getListView().addHeaderView(header);
+                profileFragment.getListView().addHeaderView(header);
             }
 
             final View dialogLayout = mSettingsDialogView;
             AlertDialog.Builder settingsDialog = new AlertDialog.Builder(activity);
-            profileFrag.setDevice(device);
+            profileFragment.setDevice(device);
             final EditText deviceName = (EditText)dialogLayout.findViewById(R.id.name);
             deviceName.setText(device.getName(), TextView.BufferType.EDITABLE);
+
+            final DeviceProfilesSettings dpsFragment = profileFragment;
+            final Context context = v.getContext();
             settingsDialog.setView(dialogLayout);
             settingsDialog.setTitle(R.string.bluetooth_preference_paired_devices);
             settingsDialog.setPositiveButton(R.string.okay,
@@ -429,7 +432,7 @@
                     device.setName(deviceName.getText().toString());
                 }
             });
-            final Context context = v.getContext();
+
             settingsDialog.setNegativeButton(R.string.forget,
                     new DialogInterface.OnClickListener() {
                 @Override
@@ -442,6 +445,16 @@
                 }
             });
 
+            // We must ensure that the fragment gets destroyed to avoid duplicate fragments.
+            settingsDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
+                public void onDismiss(final DialogInterface dialog) {
+                    if (!activity.isDestroyed()) {
+                        activity.getFragmentManager().beginTransaction().remove(dpsFragment)
+                            .commitAllowingStateLoss();
+                    }
+                }
+            });
+
             AlertDialog dialog = settingsDialog.create();
             dialog.create();
             dialog.show();
diff --git a/src/com/android/settings/bluetooth/DeviceProfilesSettings.java b/src/com/android/settings/bluetooth/DeviceProfilesSettings.java
index ee25300..5c2beba 100755
--- a/src/com/android/settings/bluetooth/DeviceProfilesSettings.java
+++ b/src/com/android/settings/bluetooth/DeviceProfilesSettings.java
@@ -232,7 +232,7 @@
                 status == BluetoothProfile.STATE_CONNECTED;
 
         if (isConnected) {
-            askDisconnect(getActivity(), profile);
+            askDisconnect(mManager.getForegroundActivity(), profile);
         } else {
             if (profile.isPreferred(device)) {
                 // profile is preferred but not connected: disable auto-connect