How to handle Activity when Orientation changes?

安稳与你 提交于 2019-12-23 21:37:41

问题


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?


回答1:


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"> 

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.

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.



来源:https://stackoverflow.com/questions/3438743/how-to-handle-activity-when-orientation-changes

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