进入景观时的setText,重置和锁
-
26-10-2019 - |
题
嘿,谢谢您的浏览。我真的不明白为什么会发生这种情况,尽管它可能与我的背景线程有关,该线程正在处理Audio Intupt。一切都很好,直到我倾斜手机,还没有放置景观肖像或类似的东西。因此,它应该自动扩展我的肖像布局。但是它确实扩展了它,它也可以重置我的标签,并且使我无法在它们上使用我的setText功能。我还注意到它暂停了我的背景线程。这是设置文本视图的代码:
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);
}
}
这是我的XML文件
<?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.
正如您所看到的那样,这并不完全正确。无与伦比的方向变化的预期行为是重新创建活动(这意味着要浏览整个活动生命周期,ondestroy(),onCreate()()等等 - 显然,您的标签重置)。这可以通过各种方式来处理:
http://developer.android.com/guide/topics/resources/runtime-changes.html & http://developer.android.com/resources/articles/faster-screen-erientation-eentation-change.html
我在应用程序中以两种简单的方式“处理”方向变化。通过在清单中设置筛网方向标志,一项活动被锁定在肖像上(在景观中查看它没有意义):
<activity android:name=".SettingsActivity" android:screenOrientation="portrait">
我使用的第二个活动需要以两种方式查看,因为它包含在景观中查看时看起来最好的图。这基本上意味着我确保我的活动优雅地关闭(包括我使用的任何线程或处理程序),并进行重新绘制以绘制图形以景观方向填充屏幕。
您需要考虑如何处理方向更改。是什么使您的应用程序最有意义?周围有很多好的资源,可以通过Google进行谈论的方向变化。
我想您在背景线程中遇到的麻烦归因于您在被摧毁和重塑的活动中所遇到的处理程序。
其他提示
当设备旋转时,它会有效地破坏并重新创建您的活动在新方向上。如果您将线程作为活动的一部分启动,那将解释您看到的一些行为。如果您希望线程在两个活动实例之间生存,那么您需要将其作为服务而不是简单的线程实现。
证明这一点的方法是设置断点或添加一些登录方法,并且在应用程序启动并首先显示活动以及设备旋转时,您会发现这两者都会被击中。
每次倾斜手机时,即每次在肖像模式和景观模式之间进行更改时,活动都会重新启动,因此 onCreate()
被执行。