Android Bluetooth crashes after orientation change
-
20-09-2019 - |
Question
My code is based on the BluetoothChat sample from the API. I connect to another device just fine and I can read and write to the connection no problem. But then if I turn the device the orientation of the UI changes automatically. After this, if I try to write to the Bluetooth connection, my application crashes!
It seems weird that the orientation of the screen somehow affects the Bluetooth. I am new to Android, and I am not yet sure about stuff like when I should be saving and reloading the application's state. Is this one of those times? Does changing the orientation of the screen somehow reset the application's state?!
Thanks!
Solution
When the orientation of the screen is changed, your activity is killed and restarted. You must save the state and reinitialize. See here: onSaveInstanceState