旋转屏幕时可用蓝牙设备的 ListView 变为空白
ListView of available Bluetooth device goes blank while rotating the screen
我有一个 activity,我在其中扫描蓝牙设备并将其放入列表视图中。当我扫描设备时显示良好,但当我旋转屏幕(纵向 <-> 横向)时,列表视图变为空白。
我已经实现了 onSaveInstanceState 方法,但我真的不知道要在 outState.put...
中保存什么
// implement the broadcast reciever for the BT scanning
private final BroadcastReceiver btScanBrcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mBTDevices.add(device);
availableDevicesList = new AvailableDevicesList(context, R.layout.device_list_view, mBTDevices);
lv_list_devices.setAdapter(availableDevicesList);
}
}
};
在onCreate方法中
//perform action when clicking on device scaning
scan_dv_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// check if the scan in in process
if(btAdapter.isDiscovering()){
// cancel the scanning
btAdapter.cancelDiscovery();
// for version greater than Lolipop
checkBTPermission();
// restart it
btAdapter.startDiscovery();
// register the Broadcast receiver
IntentFilter IntentfindDv = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(btScanBrcastReceiver, IntentfindDv);
}
if(!btAdapter.isDiscovering()){
//// for version greater than Lolipop
checkBTPermission();
// start the scanning
btAdapter.startDiscovery();
// register the Broadcast receiver
IntentFilter IntentfindDv = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(btScanBrcastReceiver, IntentfindDv);
}
}
});
可能是您的 activity 在您更改方向(纵向 <-> 横向)时正在重新创建。如果您不想在旋转屏幕时刷新 UI,请在清单文件中使用以下内容,
<activity
android:name=".MainActivity"
android:configChanges="orientation">
如果您想避免 activity 进行多项活动,请查看下方内容,
android:configChanges
Lists configuration changes that the activity
will handle itself. When a configuration change occurs at runtime, the
activity is shut down and restarted by default, but declaring a
configuration with this attribute will prevent the activity from being
restarted. Instead, the activity remains running and its
onConfigurationChanged() method is called.
https://developer.android.com/guide/topics/manifest/activity-element
android:configChanges=["mcc", "mnc", "locale",
"touchscreen", "keyboard", "keyboardHidden",
"navigation", "screenLayout", "fontScale",
"uiMode", "orientation", "density",
"screenSize", "smallestScreenSize"]
我有一个 activity,我在其中扫描蓝牙设备并将其放入列表视图中。当我扫描设备时显示良好,但当我旋转屏幕(纵向 <-> 横向)时,列表视图变为空白。
我已经实现了 onSaveInstanceState 方法,但我真的不知道要在 outState.put...
中保存什么 // implement the broadcast reciever for the BT scanning
private final BroadcastReceiver btScanBrcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mBTDevices.add(device);
availableDevicesList = new AvailableDevicesList(context, R.layout.device_list_view, mBTDevices);
lv_list_devices.setAdapter(availableDevicesList);
}
}
};
在onCreate方法中
//perform action when clicking on device scaning
scan_dv_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// check if the scan in in process
if(btAdapter.isDiscovering()){
// cancel the scanning
btAdapter.cancelDiscovery();
// for version greater than Lolipop
checkBTPermission();
// restart it
btAdapter.startDiscovery();
// register the Broadcast receiver
IntentFilter IntentfindDv = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(btScanBrcastReceiver, IntentfindDv);
}
if(!btAdapter.isDiscovering()){
//// for version greater than Lolipop
checkBTPermission();
// start the scanning
btAdapter.startDiscovery();
// register the Broadcast receiver
IntentFilter IntentfindDv = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(btScanBrcastReceiver, IntentfindDv);
}
}
});
可能是您的 activity 在您更改方向(纵向 <-> 横向)时正在重新创建。如果您不想在旋转屏幕时刷新 UI,请在清单文件中使用以下内容,
<activity
android:name=".MainActivity"
android:configChanges="orientation">
如果您想避免 activity 进行多项活动,请查看下方内容,
android:configChanges Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.
https://developer.android.com/guide/topics/manifest/activity-element
android:configChanges=["mcc", "mnc", "locale",
"touchscreen", "keyboard", "keyboardHidden",
"navigation", "screenLayout", "fontScale",
"uiMode", "orientation", "density",
"screenSize", "smallestScreenSize"]