问题
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