简体   繁体   English

如何在Android的Orintation之后不重新创建活动?

[英]How NOT to re-create activity after orintation in android?

I have activity the contains a ListView , and this listview is filled with data from a web service in onCreate() function ( using AsyncTask ). 我有一个活动,其中包含一个ListView ,并且此listview充满了onCreate()函数(使用AsyncTask )中来自Web服务的数据。 When the screen orientation changes the activity is re-created, I don't want that. 当屏幕方向更改时,将重新创建活动,我不希望那样。 how to keep saved after changing the orientation 更改方向后如何保存

在AndroidManifest.xml中为该活动设置android:configChanges="orientation"

You need to add this in the Activity in AndroidManifest.xml. 您需要在AndroidManifest.xml的“活动”中添加此代码。 Just adding "orientation" will not work on all devices. 仅添加“方向”将无法在所有设备上使用。

android:configChanges="orientation|screenSize"

Although the other answers suggest using android:configChanges , I would say this is really only a last resort option because it means resources for different orientations will not get loaded automatically. 尽管其他答案建议使用android:configChanges ,但我会说这实际上只是最后的选择,因为这意味着不同方向的资源不会自动加载。

Instead you should use the onSaveInstanceState or onRetainNonConfigurationInstance functions of your activity. 相反,您应该使用活动的onSaveInstanceStateonRetainNonConfigurationInstance函数。 (If you use fragments, you should use setRetainInstance instead of the latter option.) (如果使用片段,则应使用setRetainInstance而不是后者。)

Another option would be to use a ContentProvider to store the data you download using the AsyncTask into a database so it wouldn't need to be re-downloaded anyway. 另一个选择是使用ContentProvider将使用AsyncTask下载的数据存储到数据库中,因此无论如何都不需要重新下载它。

You should read this for more information. 您应该阅读以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM