改变方向,丢失我所有的列表项 [英] Changing Orientation, losing all my list items

查看:23
本文介绍了改变方向,丢失我所有的列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 Android 设备上测试我的应用程序时,将我的 Android 手机从横向变为纵向,结果我的列表视图中的所有列表项都消失了.

为什么?

如何管理?

解决方案

您正在丢失列表项,因为在方向更改期间 android 的默认行为是销毁您的 Activity 并重新创建它.选择此行为是为了使 android 能够使用新布局重新创建 Activity,该布局可能特别适用于新方向.

为了防止您的列表项消失,需要做几件事.在方向更改期间,第一件事是在更改后简单地重用相同的活动.这可以通过添加这一行来完成

android:configChanges="keyboardHidden|orientation"

到您遇到问题的清单中的活动标签.此问题对此进行了更详细的解释.>

如果用户打开 Activity,然后将您的应用程序发送到后台执行许多其他内存繁重的操作以将您的应用程序从内存中删除,然后重新访问您的应用程序,那么您解释的行为也可能会出现.如果是这种情况,您必须覆盖活动中的 onSaveInstanceState 方法.这个问题中解释了如何做到这一点.

While I tested my application on an Android Device turning my Android phone from landscape to portrait, results in all the list items in my list view are disappearing.

Why?

How to manage?

解决方案

You are losing the list items because the default behavior for android during orientation change is to destroy your activity and recreate it. This behavior is chosen to enable android to recreate the activity with a new layout that may be used especially for the new orientation.

To prevent your list items from disappearing there are several thing that have to be done. The first thing that helps during orientation change is to simply reuse the same activity after the change. This can be done through adding this line

android:configChanges="keyboardHidden|orientation"

to the activity tag in your manifest you are experiencing the problem with. This is explained in more detail in this question.

The behavior you explained will likely also appear if the user opens the activity and then sends your application to the background does many other memory heavy stuff to get your application removed from memory and then revisits your app. If this is the case you have to overwrite the onSaveInstanceState method in you activity. How to this is explained in this question.

这篇关于改变方向,丢失我所有的列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆