Search code examples
androidlandscapeandroid-1.5-cupcakesettext

setText, resets and locks when entering landscape


Hey all thanks for looking this through. I really can't understand why this happends, though it might have something to do with my background thread which is processing audioinput. All works fine until i tilt the phone, havn't yet put an landscape portrait or stuff like that in. So it should automatic just stretch my portrait layout. But it does just stretch it, it also resets my label, and makes me unable to use my setText features on them. I also noticed that it pauses my background thread. Here is code to set the textViews:

private void reactOnAudioMessage(Message msg) {
    TextView txtVwLastValue = (TextView) findViewById(R.id.txtVwLastValue);
    String result=null;
    DataFile newData=null;
    Bundle bndl=msg.getData();
    // ** newData getting created, with the correcy value etc this is not the problem.
    if(newData!=null){
        txtVwLastValue.setText("Last Value Received:" +newData.getValue());
        mDataList.add(newData);
        drawGraph(mImageView);
    } 
}

Here is my xml file

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Record"
android:id="@+id/btnRecord">
</Button>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Last Value Received:" android:id="@+id/txtVwLastValue"></TextView>
<TextView android:text="TextView" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/txtVwType"></TextView>
<ImageView android:src="@drawable/icon" android:layout_width="fill_parent" android:id="@+id/imgViewGraph" android:layout_height="300px"></ImageView>
</LinearLayout>

Solution

  • So it should automatic just stretch my portrait layout.
    

    This is not entirely true, as you have also seen. The expected behaviour for an unhandled orientation change is to recreate the Activity (which means going through the entire Activity lifecycle, onDestroy(), onCreate() and so forth - obviously the reason your label is being reset). This can be handled in various ways:

    http://developer.android.com/guide/topics/resources/runtime-changes.html & http://developer.android.com/resources/articles/faster-screen-orientation-change.html

    I "handle" orientation changes in 2 rather simple ways in my application. One activity is locked to portrait (as it makes no sense to view it in Landscape) by setting the screenOrientation flag in the Manifest:

    <activity android:name=".SettingsActivity" android:screenOrientation="portrait">
    

    The 2nd Activity that I use needs to be viewed in both ways as it contains a graph that looks best when viewed in Landscape. This basically means that I make sure that my Activity closes down gracefully (including any threads or handlers that I use) and is remade to draw the graph filling the screen in Landscape orientation.

    You need to think about how you wish to handle the orientation change. What makes the most sense in your application? There's a lot of good resources around that talks about orientation changes through Google.

    I imagine the trouble you are having with the background thread is due to handlers that you have in the Activity which is destroyed and remade.