如何处理活动时方向的变化? [英] How to handle Activity when Orientation changes?

查看:216
本文介绍了如何处理活动时方向的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个活动,即使用ArrayAdapter从服务器加载数据并显示它作为一个清单。对于我展示一个进度对话框即加载,而它加载从服务器上的所有数据。然后我解雇了处理程序对话框。我的问题是,当我过更改方向,进度对话框再次显示,这是没有必要的,因为已经显示所有的数据?

I am writing an activity, that loads data from a server and displays it as a list using ArrayAdapter. For that I'm showing a progress dialog i.e loading, while it loads all data from the server. Then i dismiss the dialog in a handler. My problem is that when ever i change the orientation, the progress dialog is again shown, which is not needed, because all the data is displayed already?

推荐答案

我会说你有两个选择:结果
要么你强迫你的活动不能够改变方向:

I would say you have two options :
Either you force your activity not to be able to change orientation :

<activity android:name="MainActivity" android:configChanges="keyboardHidden|orientation"> 

在你的清单(文档),它会告诉你希望系统自己处理方向变化,在这种情况下,你不会做任何事情。但是,你宁愿添加一些code在的onPause()的onSaveInstanceState()因为这个活动可能当你接到一个电话,例如可以由Android中断。所以,你需要处理一些储蓄。

in your manifest (documentation), which will tell the system that you want to handle orientation changes by yourself, and in this case, you won't do anything. But, you'd rather add some code in onPause() and onSaveInstanceState() because this activity might be interrupted by Android when you receive a phone call for instance. So you need to handle some saving.

或者,你选择prevent对话框显示已经被显示时,或者当你的后台线程运行。这是很容易的,你使用的AsyncTask您下载的一部分,因为你可以使用<一个href=\"http://developer.android.com/reference/android/os/AsyncTask.html#getStatus%28%29\">AsyncTask.getStatus这将返回任务的状态。如果它已经是成品的状态,你必须取消对话框。

Or, you choose to prevent the display of the dialog when has already been displayed, or when your background thread is running. This is easy is you use an AsyncTask for your download part, because you can use AsyncTask.getStatus which will return you the status of the task. If it is already in finished status, you have to cancel the dialog.

这篇关于如何处理活动时方向的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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