简体   繁体   中英

How to handle Activity when Orientation changes?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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