Android: Stop Recreating the activity on orientation change

五迷三道 提交于 2019-12-08 16:05:50

问题


I have a listview with two buttons in my main.xml layout. On click of one button i'am creating a textview dynamically and adding it at the bottom of the screen to confirm the user interaction. When the user clicks 2nd button (Confirm button), i need to add that text to listview. To support landscape mode, i have the same layout file in layout-land folder. When i click on 1st button it is creating a textview with some text and adding at bottom of the screen. Now if a change the device orientation then it is loading the landscape main.xml and activity is recreating again. So my textview is getting collapsed. How can i prevent that the recreation of activity on orientation change. (But it should pick up the other layout file).


回答1:


Just edit the Activity Tag in androidmanifest.xml.

<activity
            android:configChanges="keyboardHidden|orientation"
            android:name=".testActivity"
            android:label="@string/app_name"></activity>



回答2:


You should add screenSize

if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

Added in API level 13.

then it should be like this

<activity
                android:configChanges="keyboardHidden|orientation|screenSize"
                android:name=".testActivity"
                android:label="@string/app_name"></activity>

http://developer.android.com/guide/topics/manifest/activity-element.html



来源:https://stackoverflow.com/questions/10530760/android-stop-recreating-the-activity-on-orientation-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!