My app is receiving data from a BlueTooth device 4 times a second, parsing it and then displaying it to the screen. Everything works fine, until the screen orientation changes, then the textViews revert to their default values set in my onCreate and will not change from there.
I am still receiving the data from my message handler from the bluetooth thread, and the parsing is working fine, but my updateUI() method does nothing. All that is in updateUI() is setting the TextView’s text to the value, like tv_text.setText(doubleValue);. Again, this works fine before the screen gets rotated or sleeps.
Does the View ID change when the screen is rotated so my TextView is the wrong one? How do I get around this? I have seen others with problems of the TextView loosing it’s data, but I’m not sure if that’s the same problem because I am not able to set the text again after rotation.
Thanks.
EDIT: I have discovered some odd behavior.
-
If I rotate the screen before connecting to the BT device, it will work when I connect. But then it stops when I rotate again.
-
When I rotate, the text view displays the value that was last assigned to it BEFORE connecting. For example, if do tv.setText(“10”), it will display 10. Then I connect, and it updates with the correct values. Then I rotate, and it does back to displaying 10…
-
The only time I set the text values in in my updateUI() code and that is called after the message is received and parsed.
Now I’m really confused…
EDIT 2
OK, I did some more testing ans I think I may have located the cause. When I tried to disconnect the bluetooth connection I found out that it wasn’t disconnecting. So then i tried disconnecting before rotating, and then connecting again and it worked.
So I believe that when onCreate is run again, it is duplicating my bluetooth comm thread and ignoring the old one. The problem is I don’t know how to make initialize the BTComm object in a way that it will still work when the activity is destroyed and re created, it is still working and can communicate with the UI. Here is some code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (btc == null){
btc = new BTComm(this, messageHandler);
}
tv_temp1 = (TextView) findViewById(R.id.tv_temp1);
tv_temp2 = (TextView) findViewById(R.id.tv_temp2);
tv_temp1Label = (TextView) findViewById(R.id.tv_temp1Label);
tv_temp2Label = (TextView) findViewById(R.id.tv_temp2Label);
tv_status = (TextView) findViewById(R.id.tv_status);
}
and the handler that handles the messages received from the BT comm class (btc)
Handler messageHandler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what){
case BTComm.MESSAGE_CONNECTED:
Toast.makeText(LambdaActivity.this, "Connected!", Toast.LENGTH_SHORT).show();
tv_status.setText("Connected.");
mode = MODE_CONNECTED;
break;
case BTComm.MESSAGE_READ:
parse(msg.getData().getString("results"), msg.getData().getLong("time"));
break;
case BTComm.MESSAGE_DISCONNECTED:
Toast.makeText(LambdaActivity.this, "Disconnected!", Toast.LENGTH_SHORT).show();
tv_status.setText("Not connected...");
mode = MODE_NOT_CONNECTED;
break;
}
}
};
It would work if I disconnected and re-connected each time the screen was rotated, but that would interrupt the function of the app’s data logging feature so I don’t want to do that… If I lock the orientation to landscape, the screen obviously doesn’t rotate and cause this problem, but if the phone sleeps, when it wakes up in portrait more, it then rotates to landscape mode and the problem happens.
Just in case anyone else is having this problem I have found a fix.
I ended up moving my bluetooth listener to a service, and when it receives the data from the BT device, it then broadcasts it and is received by my UI activity.
Some more information can be found in my other question, here: Android background task and lifecycle