Note that there are some explanatory texts on larger screens.

plurals
  1. POScreen rotation causing TextViews to stop updating
    text
    copied!<p>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.</p> <p>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.</p> <p>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.</p> <p>Thanks.</p> <p>EDIT: I have discovered some odd behavior.</p> <ol> <li><p>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.</p></li> <li><p>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...</p></li> <li><p>The only time I set the text values in in my updateUI() code and that is called after the message is received and parsed.</p></li> </ol> <p>Now I'm really confused...</p> <p><strong>EDIT 2</strong></p> <p>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.</p> <p>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:</p> <pre><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); } </code></pre> <p>and the handler that handles the messages received from the BT comm class (btc)</p> <pre><code>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; } } }; </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload