开发者

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 t开发者_如何学运维o 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>


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.


When the device is rotated, it effectively destroys and re-creates your activity in the new orientation. If you are kicking off your thread as part of your activity, that would explain some of the behaviour that you're seeing. If you want your thread to survive between the two activity instances, then you'll need to implement it as a Service rather than a simple thread.

The way to prove this is to set a breakpoint or add some logging in your onCreate method, and you'll see this being hit both when the app starts up and the activity is first displayed, and on device rotations.


Each time you tilt your phone, i.e each time you change between portrait mode and landscape mode, the activity restarts, so onCreate() is executed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜