开发者

Android Updating UI from AsycTask after Orientation Change

开发者 https://www.devze.com 2023-04-06 02:04 出处:网络
I am using AsyncTask to retrieve some data from Server which is displayed on the screen. The issue is if I change the orientation of the device while data is being retreieved (a progress bar being dis

I am using AsyncTask to retrieve some data from Server which is displayed on the screen. The issue is if I change the orientation of the device while data is being retreieved (a progress bar being displayed), my UI does not get updated.

I have done following to handle this situation:

a) override onRetainNonConfigurationInstance which returns the instance of my AsychTask, retrieve this on onCreate method. b) Declared my widgets as volatile c) Redraw the entire U by calling setContentView from postExecute of the AsycTask d) invoked invalidate method on the widgets

I do not get any errors on screen, my application does not crashed but the UI is not updated either. I have executed the code on debug mode, the code which updates the UI is actually executed, but it does nothing.

In logs I see one error saying my Activity has leaked a window.

Any suggestions开发者_开发技巧 what I am doing wrong or not doing something which I should be doing.


The problem is almost certainly that your AsyncTask is keeping a reference to the old Activity. (Perhaps it's an inner class?) This is a surprisingly difficult problem. There's an excellent discussion of the difficulties, as well as some solutions, on this thread at Google groups. Scroll down for the answers by Lance Nanek for some code that gets around this.

The problem is not specific to AsyncTask; it can happen with any background thread that needs to interact with the UI and also needs to survive the destroy/start lifecycle events that come with a configuration change.

0

精彩评论

暂无评论...
验证码 换一张
取 消