我有一个活动,我在列表视图中扫描蓝牙设备。当我扫描设备显示良好但当我旋转屏幕(纵向< - >横向)时,列表视图变为空白。
我已经实现了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);
}
}
});
可能是您在更改方向时重新创建的活动(纵向< - >横向)。如果您不想在旋转屏幕时刷新UI,请在清单文件中使用以下内容,
<activity
android:name=".MainActivity"
android:configChanges="orientation">
如果您想避免多项活动的娱乐活动,请查看以下内容,
android:configChanges列出活动将自行处理的配置更改。在运行时发生配置更改时,默认情况下会关闭并重新启动活动,但声明具有此属性的配置将阻止活动重新启动。相反,活动仍在运行,并调用其onConfigurationChanged()方法。
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"]